C File Handling

C File handling is an important part of a software application. We mostly need to open the file and process the following tasks:

  • Opening a File
  • Reading a File
  • Writing a File
  • Closing a File
  • Deleting a File

C File Handling: Opening a File using fopen() Function

In C File Handling, with the help of fopen() function, we open a file and perform further action according to our need.

Syntax :

*fp = FILE *fopen(const char *filename, const char *mode);

Here, the filename is the name of the file to be opened and mode specifies the purpose of opening the file.
*fp is the FILE pointer (FILE *fp), which will hold the reference to the opened(or created) file.
The mode can be of following types:

  • r: Open for reading.
  • rb: Open for reading in binary mode.
  • w: Open for writing.
  • wb: Open for writing in binary mode.
  • a: Open for append. i.e, Data is added to the end of the file.
  • ab: Open for append in binary mode. i.e, Data is added to the end of the file.
  • r+: Open for both reading and writing.
  • rb+: Open for both reading and writing in binary mode.
  • w+: Open for both reading and writing.
  • wb+: Open for both reading and writing in binary mode.
  • a+: Open for both reading and appending.
  • ab+: Open for both reading and appending in binary mode.

Simple example of file opening using fopen() Function in C:

#include 
int main()
{
     /* Pointer to the file */
     FILE *fp1;
     /* Character variable to read the content of file */
     char c;

     /* Opening a file in r mode*/
     fp1= fopen ("demofile.txt", "r");
     return 0;
}

C File Handling: Closing a File using fclose() Function

With the help of fclose() function, we can close the already opened file.

int main()
{
    /* Pointer to the file */
    FILE *fp1;
    /* Opening a file in r mode*/
    fp1= fopen ("myfile.txt", "r");
	fclose(fp1); // closing a file
    return 0;
}

Writing a File using fprintf() Function

With the help of fprintf() function, we can write a set of characters into file. It sends formatted output to a stream.

Simple example of Writing in a file in C:

#include   
int main(){  
   FILE *fp;  
   fp = fopen("file.txt", "w"); // opening file  
   fprintf(fp, "Hello file by fprintf...\n");//writing data into file  
   fclose(fp);//closing file  
return 0;
}

Reading a File using fscanf() Function

With the help of the fscanf() function, we can read a set of characters from file. It reads a word from the file and returns EOF at the end of file.

Simple example of reading a file in C:

#include<stdio.h>   
int main(){  
   FILE *fp;  
   char buff[255]; // creating char array to store data of file  
   fp = fopen("file.txt", "r");  
   while(fscanf(fp, "%s", buff)!=EOF){  
   printf("%s ", buff );  
   }  
   fclose(fp);  
}

Input/Output operation on File using getc() and putc() Function:

Above we have discussed about various file I/O (input and output) functions to perform reading and writing on file. With the help of getc() and putc() functions, we can read and write individual characters to a file in C language.

Let’s see a simple example of putc() function:

#include<stdio.h>
main()
{
 FILE *fp;
 char ch;
 fp = fopen("one.txt", "w");
 printf("Enter data");
 while( (ch = getchar()) != EOF) {
    putc(ch,fp);
 }
 fclose(fp);
 fp = fopen("one.txt", "r");
 
 while( (ch = getc(fp)! = EOF)
    printf("%c",ch);
    
 fclose(fp);
}

Deleting a file in C

With the help of remove() function, we delete a file. Before deleting a file , make sure that file is present in our directory.

Simple example of deleting a file in C:

#include 
 int main()
{
  int result;
  char file[25];
 
  printf("Enter name of a file you wish to delete \n");
  gets(file);
 
  result = remove(file);
 
  if (result == 0)
    printf("%s file deleted successfully. \n", file);
  else
  {
    printf("Unable to delete the file. \n");
  }
 return 0;
}

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.