When you read a text file in c, you usually need to process the file line by line. For example, you may want to search for specific keywords, count the number of occurrences of each keyword, or calculate the average length of a word in the file. In this tutorial, we’ll demonstrate how to read a text file line by line using a C program. First, we’ll need a sample text file to analyze. Next, we’ll create a function to read the file, print the contents of the file, and finally write the data back to the file. To make things interesting, we’ll also read and write the data in binary format. You should be able to understand the code for yourself, but if you need any help, you can always check out the video tutorials linked below.
In most, practices used programs information needs to be stored in computer memory devices for future use and analysis. But the common console input/output programs are not capable of storing data on a computer hard disk. In order to create a file on a computer memory device and to extract the information from the file, the concept of file handling in C plays a vital role. In this tutorial post, I have described the process of opening and reading a text file using file handling.
This tutorial will show you how to open and read a text file in C. This is a very useful skill, as it allows you to access data from the file. You can then use this data to manipulate your program in any way you like.
You will need to know some basic concepts of programming before reading this tutorial. If you do not know what these are, then please read my Introduction to Programming articles in the C Programming category.
The tutorial post consists of two separate source codes for opening and reading a text file along with a brief explanation of each algorithm. The algorithm presented gives the ides for the 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 a Text File in C
- For opening a text file in C
- Start
- Declare variable and file pointer
- Assign file pointer to fopen() function with write format
- Print an error message If the file is not opened
- Else give the content using loop
- Close the file
- Stop
- For Reading a Text File
- Start
- Declare variables and the file pointer
- Open the file
- If the file is not opened print error massage
- Print the content of the text file using the loop
- Close the file
- Stop
Source Code for Opening and Reading a text file
- For Opening a Text File in C
#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 that 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 in C
#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 the 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 arguments. After the file is opened in reading mode, the content of file.txt is displayed on the screen, and the 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 the Code::Blocks compiler, the first code opens the file on the hard disk and the second reads the content of the file.
Conclusion:
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 these source codes as a part of your C programming project to open files.