What are the different operations on files in C language?

CServer Side ProgrammingProgramming

The operations that can be carried out on files in C language are as follows −

  • Naming the file.
  • Opening the file.
  • Reading from the file.
  • Writing into the file.
  • Closing the file.

Syntax

The syntax for opening and naming file is as follows −

FILE *File pointer;

For example, FILE * fptr;

File pointer = fopen ("File name”, "mode”);

For example, fptr = fopen ("sample.txt”, "r”)

FILE *fp;
fp = fopen ("sample.txt”, "w”);

Modes of opening the file

The modes of opening the file in C language are explained below −

ModesDescription
rFile is opened for reading
wFile is opened for writing
a+File opened for append
r+File opened for reading & writing
w+File opened for writing & reading
a+File opened for append & reading
rtText file is opened for reading
wtText file is opened for writing
atText file is opened for appending
r+tText file is opened for reading & writing
w+tText file is opened for both writing & reading
a+tText file is opened for both appending & reading
rbBinary file is opened for reading
wbBinary file is opened for writing
abBinary file is opened for appending
r+bBinary file is opened for both reading & writing
w+bBinary file is opened for both writing & reading
a+bBinary file is opened for both appending & reading.
  • Write mode of opening the file

The syntax is as follows −

FILE *fp;
fp =fopen ("sample.txt”, "w”);

If the file is not existing, then a new file is created.

If the file exists then, old content gets erased and current content will be stored.

  • Read mode of opening the file

The syntax is as follows −

FILE *fp
fp =fopen ("sample.txt”, "r”);

If the file is not existing, then fopen function returns NULL value.

If the file exists, then data is read successfully from file

  • Append mode of opening a file

The syntax is as follows −

FILE *fp;
fp =fopen ("sample.txt", "a");

If the file doesn’t exist, then a new file will be created.

If the file exists, the current content will be adding to the old content.

ModeExitNot Exit
RReadfp="NULL"
WCurrent ContentNew file will be created
AOld ContentCurrent Content
New file will be Created

Example

Following is the C program for operations on files −

 Live Demo

//Program for copying the contents of one file into another file
#include <stdio.h>
#include <stdlib.h> // For exit()
int main(){
   FILE *fptr1, *fptr2;
   char filename[100], c;
   printf("Enter the filename to open for reading \n");
   scanf("%s",filename);
   // Open one file for reading
   fptr1 = fopen(filename, "r");
   if (fptr1 == NULL){
      printf("Cannot open file %s \n", filename);
      exit(0);
   }
   printf("Enter the filename to open for writing \n");
   scanf("%s", filename);
   // Open another file for writing
   fptr2 = fopen(filename, "w");
   if (fptr2 == NULL){
      printf("Cannot open file %s \n", filename);
      exit(0);
   }
   // Read contents from file
   c = fgetc(fptr1);
   while (c != EOF){
      fputc(c, fptr2);
      c = fgetc(fptr1);
   }
   printf("\nContents copied to %s", filename);
   fclose(fptr1);
   fclose(fptr2);
   return 0;
}

Output

When the above program is executed, it produces the following result −

Enter the filename to open for reading
file2.txt
Enter the filename to open for writing
file1.txt
Contents copied to file1.txt
raja
Published on 09-Mar-2021 10:45:57
Advertisements