At first, we will see a programming example of how file handling can be done in the C language.
Programming Example 1
At first, we will see a programming example of how file handling can be done in the C language.
int main () {
int i;
FILE *fp;
char s [100];
fp = fopen ("f1.txt","w"); // file opening mode.
if (fp == NULL) {
printf (" File can not open. ");
exit (1);
}
printf (" Enter a string: ");
gets (s); // string enters by the user.
for (i=0; i<strlen (s); i++); {
fputc (s[i], fp); // prints the each string.
}
fclose(fp); // file is closed.
return 0 ;
}
Output
Explanation
Here we explain this particular program with a diagram. Here we can show how the file is transferred and loaded, and handled in the memory.
Diagram: File Handling
If we want to write something on a file, that file exists in Hard disk name f1.text; then we have to open the file through fopen (); it makes an image of the file f1 in the RAM that is called Buffer. It is an intermediate location where we write when Buffer is full; it goes to automatic in the Hard Disk. If the buffer is not full, fclose () transfer our file into the Hard Disk.
Now we discuss different types of modes available in the C language.
File Opening Modes
Mode | Meaning | Description |
r | Read | Only reading possible. No, create the file if it does not exist. |
w | Write | Only writing is possible. Create the file if it does not exist; otherwise, erase the old content of the file and open a blank file. |
a | Append | Only writing is possible. Create a file; if it does not exist, otherwise open the file and write from the end of the file. (Do not erase the old content). |
r+ | Reading + Writing | Reading and writing are possible. Create a file if it does not exist, overwriting existing data. Used for modifying content. |
w+ | Reading + Writing | Reading and writing are possible. Create a file if it does not exist. Erase old content. |
a+ | Reading + Appending | Reading and writing are possible. Create a file if it does not exist. Append content at the end of the file. |
Programming Example 2
This programming example shows us how a file is opened in read mode.
Output
Explanation
Here we open a file named “myfile.dat” in “r” mode. As I know from the file opening mode table, if we open a file in read mode, the file must exist. Otherwise, an error will have occurred. In this program, “myfile.dat” exists. For this, that file will open, and we will read the line from that file.
Programming Example 3
This programming example shows us how a file is opened in write mode.
Output
Explanation
Here we open a file named “write.txt” in “w” mode. As I know from the file opening mode table, only writing is possible if we open a file in write mode. If the file does not exist, this mode opens a new file, and writing is possible on that file. In this mode, erase all the old contents of the file.
Programming Example 4
This programming example shows us how a file is opened in append mode.
Output
Explanation
Here we open a file named “data.txt” in “a” mode. As I know from the file opening mode table, only writing is possible if we open a file in append mode. If the file does not exist, this mode opens a new file, and writing is possible on that file. In this mode, do not erase the old content.
Programming Example 5
This programming example shows us how a file is opened in “r+” mode. “r+” means reading + writing.
Output
Explanation
Here we open a file named “data.txt” in “r+” mode. As I know from the file opening mode table, if we open a file in reading + writing mode, create a file if the file does not exist. It overwrites the existing data. This mode is used for modifying the data content.
Programming Example 6
This programming example shows us how a file is opened in “w+” mode. “w+” means reading + writing is possible on the file.
Output
Explanation
Here we open a file named “myfile.txt” in “w+” mode. As I know from the file opening mode table, if we open a file in reading + writing mode, create a file if the file does not exist. It erases the old content of the file.
Programming Example 7
This programming example shows us how a file is opened in “a+” mode. “a+” means reading + appending is possible on the file.
#include <stdlib.h>
int main () {
FILE * fp;
char chr;
fp= fopen (" myfile.txt "," a+ "); // file opening mode.
if (fp == NULL) {
printf (" file does not exist ");
exit (1);
}
fputs ("Good morning",fp);
rewind(fp);
while ( !feof (fp)) {
chr=fgetc (fp);
printf ( "%c ", chr);
}
fclose (fp); // close the file.
return 0 ;
}
Output
Explanation
Here we open a file named “myfile.txt” in “a+” mode. As I know from the file opening mode table, if we open a file in reading + appending mode, create a file if the file does not exist. It appends the content data at the end of the file.
Conclusion
We can see different types of file opening modes in the C language. Each opening mode has its own function and is used according to the program’s requirement. These mechanisms help the programmers to handle the files efficiently.