Home >>C Tutorial >C File Handling
In order to modify (create, delete, read) the files that are stored through the created C program on the local file system file handling in C is used.
The need of the file handling in C is generated because there is a need of generating some specific input data to a several number of time in the programming and only displaying the data on the console is not enough sometimes. Since, only a limited amount of data can be displayed on the console and we all know that data that is to displayed may be big and as a known fact the memory is volatile hence, recovering the programmatically generated data repeatedly is next to impossible task. But it can be done by storing the data on the local file system that is also known to be volatile and the file can be accessed each and every time. Hence, the need of file handling in C is generated.
Here are the list of operations that can be performed on a file:
Function | Description |
---|---|
fopen() | This function is used to open a new or existing file |
fclose() | This function is used to close the file |
fseek() | This function is used to set the file pointer to given position |
fputw() | This function is used to write an integer to file |
fscanf() | This function is used to read data from the file |
fprintf() | This function is used to write data into the file |
fgetw() | This function is used to read an integer from file |
fputc() | This function is used to write a character into the file |
ftell() | This function is used to return current position |
fgetc() | This function is used to read a character from file |
rewind() | This function is used to set the file pointer to the beginning of the file |
In order to read, write or update a file, it must be opened first. The file is opened by the use of fopen() function. Here is the syntax of the fopen():
FILE *fopen( const char * filename, const char * mode );
There are generally two parameters that are accepted by the fopen() function:
Mode | Description |
---|---|
rb | This mode is used to open a binary file in read mode |
r+ | This mode is used to open a text file in read and write mode |
w+ | This mode is used to open a text file in read and write mode |
a+ | This mode is used to open a text file in read and write mode |
wb | This mode is used to open a binary file in write mode |
a | This mode is used to open a text file in append mode |
r | This mode is used to open a text file in read mode |
ab | This mode is used to open a binary file in append mode |
rb+ | This mode is used to open a binary file in read and write mode |
wb+ | This mode is used to open a binary file in read and write mode |
w | This mode is used to open a text file in write mode |
ab+ | This mode is used to open a binary file in read and write mode |
Example of fopen()
#include<stdio.h> void main( ) { FILE *fp ; char ch ; fo = fopen("helloworld.c","r") ; while ( 1 ) { ch = fgetc ( fo ) ; if ( ch == EOF ) break ; printf("%c",ch) ; } fclose (fo ) ; }
As a matter of fact, after all the operation are performed on a file then it must be closed. In order to close a file fclose() function is used.
Here is the syntax for the same:
int fclose( FILE *fp );