In C programming, when the program is closed, all the data and results are lost. In order to keep that enormous amount of data safe from getting deleted and used for future purpose, we need to form files to save those data into secondary memory. For these, different commands were needed to solve our purpose of creating, editing and closing of a file.
There are a lot of functions in C language for handling of input and output of a file. Here we’ll show you some of important commands for you to increase your understanding with C language programming.
High level file I/O functions can be divided into 2 categories as:
1. Text file
2. Binary file
File Operations
1. Create a new file
2. Open an existing file
3. Read from and write information to a file
4. Close a file
Working with file
To working with a file, we need to declare pointer of that type of file. This declaration is required to communicate between a file and a program.
FILE *ptr;
Opening a file
In C language, a file is opened using a library function fopen().
The syntax used for opening a file in standard I/O is:
ptr=fopen("fileopen","mode")
For Example:
fopen("E:\\cprog\file.txt","w");
Here, E:\\cprog\file.txt is the location where file is to be created while "w" represents the mode for writing.
Here, the program.txt file is opened for writing mode.
File
Mode
|
Meaning
of Mode
|
During
Inexistence of that file
|
r
|
Open
for reading
|
fopen()
returns NULL
|
w
|
Open
for writing
|
File
will be created
|
a
|
Open
for edit/append
|
File
will be created
|
r+
|
Open
for both reading and writing
|
fopen()
returns NULL
|
w+
|
Open
for both reading and writing
|
File
will be created
|
a+
|
Open
for both reading and appending
|
File
will be created
|
Close a File
A file is to be closed after reading or writing of a file. To close a file, a library function fclose() is used.
fclose(ptr);
Here, ptr is a file pointer which is used to associate the program with the file to be closed.
The Functions fprintf() and fscanf()
The functions of fprintf() and fscanf() is same as that of printf() and scanf(). The difference lies between them is that fprintf() and fscanf() are used to refer to a particular file.
Writing to a file
Here is an example of writing a file:
#include <stdio.h>
int main()
{
int n;
FILE *fptr;
fptr=fopen("C:\\prog.txt","w");
if(fptr==NULL)
{
printf("Error!");
exit(1);
}
printf("Enter n: ");
scanf("%d",&n);
fprintf(fptr,"%d",n);
fclose(fptr);
return 0;
}
In this program it takes a number from user and stores it into a file. After the program is compile and run, we will see can a text file prog.txt created in C drive of our computer. When we will open that file, we’ll see the integer we entered.
Similarly, fscanf() function is used to read data from a file.
Reading from file
Example of file reading
#include <stdio.h>
int main()
{
int n;
FILE *fptr;
if ((fptr=fopen("C:\\program.txt","r"))==NULL)
{
printf("Error! opening file");
exit(1);
}
fscanf(fptr,"%d",&n);
printf("Value of n=%d",n);
fclose(fptr);
return 0;
}
If you have run program above to write in file successfully, you can get the integer back entered in that program using this program.
Other functions like fgetchar(), fputc() etc. can be used in similar way.
No comments:
Post a Comment