Reading from a file means retrieving data that has already been stored in a file. In C, a file is first opened in an appropriate mode and then functions such as fgetc(), fgets() or fscanf() can be used to read its contents.
Reading from a file means retrieving data that has already been
stored in a file. In C, a file is first opened in an appropriate
mode and then functions such as fgetc(),
fgets() or fscanf() can be used to
read its contents.
The file student.txt is opened in read mode so that
its existing contents can be read.
After a file is opened successfully, the program works with the file through its file pointer. The pointer keeps track of the current position from which the next data will be read.
After opening the file, fp is used for subsequent
reading operations.
The fgetc() function reads one character from the current position of an opened file. After reading the character, the file position moves forward to the next character.
One character is read from the file referred to by
fp.
A file can be read character by character by repeatedly calling
fgetc(). The reading continues until the end of the
file is reached.
Each character is read and displayed until
fgetc() indicates that the end of the file has
been reached.
EOF is used to indicate that the end of the file
has been reached during a reading operation. When fgetc()
reaches the end of the file, it returns EOF, allowing
the loop to stop.
The loop continues while the returned character is not
EOF.
The value returned by fgetc() is commonly stored in
an int variable because the function must be able to
represent every possible character value as well as the special
EOF value.
The int variable can hold both an input character
value and EOF.
The fgets() function is used to read a string or
line of text from a file. Unlike fgetc(), which reads
one character at a time, fgets() can read multiple
characters into a character array.
A line of text is read from the file into the character array
line.
The fscanf() function is used to read formatted
data from a file. It works similarly to scanf(), but
the input is taken from a file through the file pointer.
Formatted values are read from the file represented by
fp.
Before reading a file, the program should verify that
fopen() returned a valid file pointer. If the file
cannot be opened, the function returns NULL.
This prevents the program from attempting to read from an invalid file pointer.
After all reading operations are finished, the file should be closed using fclose(). Closing the file releases the resources associated with the opened file.
Write a C program to open an existing text file and display its contents character by character.
fgetc().EOF to detect the end of the file.
Open student.txt using "r" mode, use
fgetc() inside a loop, and continue until
EOF is returned.
The fgetc() function reads one character at a time
from the current file position. Every successful read advances
the position to the next character. When the end of the file is
reached, fgetc() returns EOF.
File Contents:
Welcome to BCA Study Portal.
C Programming is easy with practice.
In this program, fp refers to the opened file and
fgetc() reads one character at a time. The loop ends
when EOF is returned.
When a file contains structured records, formatted reading can be
performed using fscanf(). This allows values such as
names, ages, and marks to be read according to a specified format.
The statement reads a string, an integer and a floating-point value from the file.
Student Records:
Name = Rahul, Age = 20, Marks = 78.5
Name = Priya, Age = 21, Marks = 86.0
Name = Aman, Age = 20, Marks = 81.5
fscanf() is useful when the file contains data in a
known format. The program checks whether all three expected values
were successfully read before processing the record.
| Function | Main Use |
|---|---|
| fgetc() | Reads one character from a file. |
| fgets() | Reads a line or string from a file. |
| fscanf() | Reads formatted data from a file. |
| feof() | Tests whether the end-of-file indicator is set. |
fgetc() → reads one character at a time.
fgets() → reads a string or line into a character array.
Trying to read from a file without checking whether
fopen() returned NULL.
Using a character variable for the result of fgetc()
can make it impossible to distinguish every valid character value
from EOF. Use an int variable when
checking for EOF.
Forgetting to close the file after completing the reading operation.
| Concept | Remember |
|---|---|
| Read Mode | "r" |
| File Pointer | FILE *fp |
| Single Character | fgetc(fp) |
| String / Line | fgets() |
| Formatted Data | fscanf() |
| End of File | EOF |
| Close File | fclose(fp) |
fgetc()?fgetc() commonly stored in an int variable?fgetc() and fgets()?fscanf()?fopen() be checked for NULL?fgetc() and EOF
with a suitable example.
fgetc(),
fgets() and fscanf().
Watch a beginner-friendly explanation of reading files in C.
A short handwritten-style revision sheet for reading from files will be provided here.
Use the mind map for file pointer, fgetc(), EOF, fgets(), fscanf() and fclose().