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 −
| Modes | Description |
|---|---|
| r | File is opened for reading |
| w | File 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 |
| rt | Text file is opened for reading |
| wt | Text file is opened for writing |
| at | Text file is opened for appending |
| r+t | Text file is opened for reading & writing |
| w+t | Text file is opened for both writing & reading |
| a+t | Text file is opened for both appending & reading |
| rb | Binary file is opened for reading |
| wb | Binary file is opened for writing |
| ab | Binary file is opened for appending |
| r+b | Binary file is opened for both reading & writing |
| w+b | Binary file is opened for both writing & reading |
| a+b | Binary 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.
| Mode | Exit | Not Exit |
|---|---|---|
| R | Read | fp="NULL" |
| W | Current Content | New file will be created |
| A | Old ContentCurrent Content | New file will be Created |
Example
Following is the C program for operations on files −
//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