How to Read Files in C: A Guide to File Operations
Learn how to read files in C programming, building on our previous chapter about writing to files using w
and a
modes with the fopen()
function. This chapter will focus on the r
mode, enabling you to effectively retrieve data from files and utilize it in your programs.
Read Files in C
Reading a File
In the previous chapter, we learned how to write to a file using the w
and a
modes in the fopen()
function. Now, we will explore how to read from a file using the r
mode.
Example
#include
int main() {
FILE *fptr;
// Open a file in read mode
fptr = fopen("example.txt", "r");
return 0;
}
This code opens example.txt
for reading. Now, let's proceed step-by-step to read the content of this file.
Creating a String to Store Content
We need to create a string that is large enough to hold the content of the file. For instance, let's make a string that can store up to 100 characters:
Example
#include
int main() {
FILE *fptr;
// Open a file in read mode
fptr = fopen("example.txt", "r");
// Store the content of the file
char myString[100];
return 0;
}
Reading the File Content
To read the content of example.txt
, we can use the fgets()
function, which reads a line of text from the file:
Example
#include
int main() {
FILE *fptr;
// Open a file in read mode
fptr = fopen("example.txt", "r");
// Store the content of the file
char myString[100];
// Read the content and store it inside myString
fgets(myString, 100, fptr);
// Print the file content
printf("%s", myString);
// Close the file
fclose(fptr);
return 0;
}
Output
If example.txt
contains "Hello Folks!", the output will be:
Hello Folks!
Reading All Lines in a File
To read every line in the file, you can use a while
loop:
Example
#include
int main() {
FILE *fptr;
// Open a file in read mode
fptr = fopen("example.txt", "r");
// Store the content of the file
char myString[100];
// Read the content and print it
while(fgets(myString, 100, fptr)) {
printf("%s", myString);
}
// Close the file
fclose(fptr);
return 0;
}
Output
If example.txt
contains:
Hello Folks!
How are you?
Good Practice
If you try to open a file for reading that does not exist, the fopen()
function will return NULL
. To handle this gracefully, you can use an if
statement to check for NULL
and print a message if the file does not exist:
Example
#include
int main() {
FILE *fptr;
// Open a file in read mode
fptr = fopen("nonexistent.txt", "r");
// Print some text if the file does not exist
if (fptr == NULL) {
printf("Not able to open the file.");
}
// Close the file
fclose(fptr);
return 0;
}
Output
If the file does not exist, the output will be:
Not able to open the file.
Combining Everything into One Example
We can combine the file reading logic with error handling as follows:
Example
#include
int main() {
FILE *fptr;
// Open a file in read mode
fptr = fopen("example.txt", "r");
// Store the content of the file
char myString[100];
// If the file exists
if (fptr != NULL) {
// Read the content and print it
while (fgets(myString, 100, fptr)) {
printf("%s", myString);
}
// If the file does not exist
} else {
printf("Not able to open the file.");
}
// Close the file
fclose(fptr);
return 0;
}
Output
If example.txt
contains:
Hello Folks!
How are you?