17 Jul C – File Handling
In this lesson, let us understand how to handle files in C. We will cover the following concepts:
- What is File Handling
- Create and close a file
- Write to a File
- Read a File
What is File Handling
File Handling in C includes creating, reading, and writing a file. It also includes deleting a file. To work on files in C programming, we have the fopen() method which is a part of the <stdio.h> header file. To use it, declare a pointer of data type FILE.
Before that, let us first see the syntax of the fopen() method:
1 2 3 |
fopen(file_name, mode); |
Here, file_name is the name of the file you want to create.
The mode is the read mode, write, and append. Only a single character represents the mode:
- r – read a file
- w – write to a file
- a – append to a file
We will create the following file pointer. Here, *fp is a pointer. FILE is a data type defined in the standard I/O library stdio.h that represents a file:
1 2 3 |
FILE *fp; |
The * i.e., the asterisk is a pointer that indicates the fp is a pointer to a FILE object. Here, fp is the name of the file pointer variable.
For more on pointers: Pointers in C
Create and close a File
To create a file in C programming, use the fopen() method and the mode w. Always remember to close an opened file using fclose() method.
Let’s say the name of the file we want to create is myfile.txt at the following location. Use the double backslash in VS Code (Windows) for the path as shown below:
1 2 3 |
E:\\Amit\\myfile.txt |
Let us now see an example to create a file in C:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <stdio.h> int main() { FILE *fp; // Create a file myfile.txt fp = fopen("E:\\Amit\\myfile.txt", "w"); // Close the file using fclose() fclose(fp); return 0; } |
The file myfile.txt gets created at the location. The file is empty since we did not add any text to it:
Write to a File
To write to a file in C, use the fprintf() method, but before that, open the file in w mode like we saw above. Set the pointer in fprintf() and the text you want to write to the new file myfile2.txt.
We are creating a new file myfile2.txt in the same folder.
Let us now see the C program to write text to the file myfile2.txt:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <stdio.h> int main() { FILE *fp; // Open a file myfile2.txt fp = fopen("E:\\Amit\\myfile2.txt", "w"); // Writing text fprint(fp, "This is a demo text."); // Close the file using fclose() fclose(fp); return 0; } |
The following will be output, i.e., the file written successfully. The myfile2.txt file has the following text as shown below:
Read a File
To read a file in C Programming, use the fopen() method and the mode r. Always remember to close an opened file using fclose() method.
We will read the above myfile2.txt with the following text:
The fgets method will be used to read a string from a file. It reads up to n-1 characters from the fils stream and stores them in to the buffer pointed to by str. Here is the syntax:
1 2 3 |
char *fgets(char *str, int n, FILE *stream); |
Here,
- str is the pointer to the array where the read string will be stored.
- n is the maximum number of characters to read (including the null terminator).
- stream is the pointer to the file from which you want to read.
Let us now see the example to read the above file i.e. myfile2.txt that already exists:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
// Read a file in C #include <stdio.h> int main() { FILE *fp; // Opena file and read with the mode r fp = fopen("E:\\Amit\\myfile2.txt", "r"); // Store the content of the file char str[30]; // Read the file and display // We have set the maximum number of characters to be read as 30 while(fgets(str, 30, fp)) { printf("%s", str); } // Close the file fclose(fp); return 0; } |
Output
1 2 3 |
This is a demo text. |
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments