In most of the practically used programs information needs to be stored in computer memory device for future use and analysis. But the common console input/output programs are not capable of storing data in computer hard disk. In order to create a file on computer memory device and to extract the information from the file, the concept of file handling in C plays vital role. In this tutorial post, I have described the process of opening and reading a text file using file handling.
The tutorial post consists of two separeate source codes for opening and reading a text file along with the brief expalnation of each and algorithm. The algorithm presented gives the ides for writing of source codes in any high level language. The algorithm for opening and reading a text file in C has given below:
Algorithm for Opening and Reading the text file
- For opening a text file
- Start
- Declare variable and file pointer
- Assign file pointer to fopen() function with write format
- If file is not opened, print error message
- Else give the content using loop
- Close the file
- Stop
- For Reading a Text File
- Start
- Declare variables and file pointer
- Open the file
- If the file is not opened print error massage
- Print the content of the text file using loop
- Close the file
- Stop
Source Code for Opening and Reading the text file
- For Opening a Text File
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include<stdio.h> int main() { FILE *fp; // declaration of file pointer int x; // declaration of variable fp =fopen("file.txt", "w"); // opening of file if (!fp) // checking of error return 1; for (x=1; x<=10; x++) fprintf(fp,"%d\n", x); // giving conten fclose(fp); // closing of file return 0; } |
The above source code is simple to understand and friendly due to the use of multiple comment lines. It includes a single header file: stdiio.h which controls the basic input/output function in the C program.
In the program, fp is the file pointer which is assigned to fopen(“file.txt”, “w”) to open a file named ‘file’ of .txt format in write mode. If the file doesn’t exist on the computer memory device, it creates a new vacant file. The statement if (!fp) is included to check the error in opening and reading a text file process.
Finally, the opened file needs to be closed and it is done with the function fclose(fp).
- For Reading a Text File
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include<stdio.h> int main() { FILE *fp; // declaration of file pointer char con[1000]; // variable to read the content fp =fopen("file.txt","r");// opening of file if (!fp)// checking for error return 1; while (fgets(con,1000, fp)!=NULL)// reading file content printf("%s",con); fclose(fp); // closing file return 0; } |
The working mechanism of this source code is similar to above source code. But, it uses the function fopen() to open the file.txt file in reading mode i.e. function is called with “file.txt” and “r” as argument. After the file is opened in reading mode, the content of file.txt is displayed on the screen and file is closed.
Both the source codes are well tested and error free. When the codes for opening and reading a text using file handing are executed in Code::Blocks compiler, the first code opens the file on hard disk and the second reads the content of the file.
As far as I think, the above source codes for opening and reading a text in C are useful in understanding the file handing process. You can use this source codes as a part of your C programming project to open file.