In C, reading the contents of a file involves opening the file, reading its data, and then processing or displaying the data.
Example
Input : File containing “This is a test file.\nIt has multiple lines.”
Output : This is a test file.
It has multiple lines.Explanation : The program reads and displays the multiline text from the file.
In this article, we will learn different methods to read the content of the whole file in C using file handling.
In C, reading a file is a step-by-step process in which we first have to prepare the file only after which we can start reading. The following are the steps that show how to read a file in C:
C programming language supports four pre-defined functions to read contents from a file, all of which are defined in header:
Table of Content
fgetc() functions reads a single character pointed by the file pointer. On each successful read, it returns the character (ASCII value) read from the stream and advances the file pointer to the next character. It returns a constant EOF (-1) when there is no content to read or an unsuccessful read.
We can read the whole content of the file using this function by reading characters one by one till we encounter NULL:
fgetc(file_ptr);
test.txt
GeeksforGeeks | A computer science portal for geeks
Output:
Content of the file are:-
GeeksforGeeks | A computer science portal for geeks
Reading file using fgetc() is useful for processing each character individually, such as counting specific characters or handling text encoding. It is also useful to print data when you don’t know anything about the file.
The fgets () function is similar to the fgetc() but instead of a single character, it reads one string at a time. It returns the string if it is successfully read or returns NULL if failed.
fgets(str, size, file_ptr);
test.txt
GeeksforGeeks | A computer science portal for geeks
Output
Content of this file are::
GeeksforGeeks | A computer science portal for geeks
Reading file using fgets() is ideal for text files where lines need to be processed individually, such as reading configuration files or log files.
fscanf() is similar to scanf() that reads the input in the form of formatted string. It can take, ignore, modify the types of the variables using the scanset characters.
int fscanf(FILE *ptr, const char *format, . )
test.txt
Raman 12
Kunal 25
Vikas 65
Output
Contents of the File are:
Name: Raman Age: 12
Name: Kunal Age: 25
Name: Vikas Age: 65
Reading a file using fread() is best for structured data files, such as CSV files or files with fixed formats (e.g., reading a list of records with specific fields).
fread () makes it easier to read blocks of data from a file. For instance, in the case of reading a structure from the file, it becomes an easy job to read using fread because instead of looking for types, it reads the blocks of data from the file in the binary form.
size_t fread(buffer_ptr, size, nmemb, file_ptr)
If the value of size or count is equal to zero, then this program will simply return 0.
test.txt
// Binary form of structure
Output
Course Name = Data Structures and Algorithms - Self Paced Price = 6000
Reading a file using fread() is suitable for binary files or when you need to manipulate the entire file content at once, such as image files or raw data files.
fgets() reads a whole line from the file at once, while fgetc() reads one character at a time.
fscanf() reads formatted input, making it ideal for structured data, whereas fgets() reads raw strings, usually one line at a time.
Closing a file using fclose() is important to free up resources and avoid memory leaks in your program.
Yes, you can use these methods to read files interchangeably, but each is suited for different scenarios, so it’s best to choose the one that matches your file type and data processing needs.