File Handling in C

In programming, we may require some specific input data to be generated several numbers of times. Sometimes, it is not enough to only display the data on the console. The data to be displayed may be very large, and only a limited amount of data can be displayed on the console, and since the memory is volatile, it is impossible to recover the programmatically generated data again and again. However, if we need to do so, we may store it onto the local file system which is volatile and can be accessed every time. Here, comes the need of file handling in C.

File handling in C enables us to create, update, read, and delete the files stored on the local file system through our C program. The following operations can be performed on a file.

  • Creation of the new file
  • Opening an existing file
  • Reading from the file
  • Writing to the file
  • Deleting the file

Consider the following example which opens a file in write mode.

  1. #include<stdio.h>  
  2. void main( )  
  3. {  
  4. FILE *fp ;  
  5. char ch ;  
  6. fp = fopen("file_handle.c","r") ;  
  7. while ( 1 )  
  8. {  
  9. ch = fgetc ( fp ) ;  
  10. if ( ch == EOF )  
  11. break ;  
  12. printf("%c",ch) ;  
  13. }  
  14. fclose (fp ) ;  
  15. }  

Output

The content of the file will be printed.

#include;
void main( )
{
FILE *fp; // file pointer
char ch; 
fp = fopen("file_handle.c","r");
while ( 1 )
{
ch = fgetc ( fp ); //Each character of the file is read and stored in the character file.  
if ( ch == EOF )
break;
printf("%c",ch);
}
fclose (fp );
}

ModeDescription
ropens a text file in read mode
wopens a text file in write mode
aopens a text file in append mode
r+opens a text file in read and write mode
w+opens a text file in read and write mode
a+opens a text file in read and write mode
rbopens a binary file in read mode
wbopens a binary file in write mode
abopens a binary file in append mode
rb+opens a binary file in read and write mode
wb+opens a binary file in read and write mode
ab+opens a binary file in read and write mode

Writing File : fprintf() function

The fprintf() function is used to write set of characters into file. It sends formatted output to a stream.

Syntax:

  1. int fprintf(FILE *stream, const char *format [, argument, ...])  

Example:

  1. #include <stdio.h>  
  2. main(){  
  3.    FILE *fp;  
  4.    fp = fopen("file.txt""w");//opening file  
  5.    fprintf(fp, "Hello file by fprintf...\n");//writing data into file  
  6.    fclose(fp);//closing file  
  7. }  

Writing File : fputc() function

The fputc() function is used to write a single character into file. It outputs a character to a stream.

Syntax:

  1. int fputc(int c, FILE *stream)  

Example:

  1. #include <stdio.h>  
  2. main(){  
  3.    FILE *fp;  
  4.    fp = fopen("file1.txt""w");//opening file  
  5.    fputc('a',fp);//writing single character into file  
  6.    fclose(fp);//closing file  
  7. }  

Reading File : fgetc() function

The fgetc() function returns a single character from the file. It gets a character from the stream. It returns EOF at the end of file.

Syntax:

  1. int fgetc(FILE *stream)  

Example:

  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. void main(){  
  4. FILE *fp;  
  5. char c;  
  6. clrscr();  
  7. fp=fopen("myfile.txt","r");  
  8.   
  9. while((c=fgetc(fp))!=EOF){  
  10. printf("%c",c);  
  11. }  
  12. fclose(fp);  
  13. getch();  
  14. }  

myfile.txt

this is simple text message

Closing File: fclose()

The fclose() function is used to close a file. The file must be closed after performing all the operations on it. The syntax of fclose() function is given below:

  1. int fclose( FILE *fp );