fgetc() Function
The fgetc()
function in C reads a single character from an input stream and advances the internal file position indicator. It is often used for reading files character by character, detecting the end-of-file, or checking for read errors.
Syntax of fgetc()
int fgetc(FILE *stream);
Parameters
Parameter | Description |
---|---|
stream | A pointer to a FILE object that identifies the input stream to read from. |
It is important to note that fgetc()
returns the character read (promoted to an int) on success. When the end-of-file is reached or an error occurs, it returns EOF
and sets the appropriate indicator (feof or ferror).
Return Value
On success, the character read is returned (as an int). If the end-of-file is reached, or if a read error occurs, fgetc()
returns EOF
and sets the corresponding indicator for the stream.
Examples for fgetc()
Example 1: Reading a Single Character from a File
This example demonstrates how to open a file, read a single character using fgetc()
, and then close the file.
Program
#include <stdio.h>
int main() {
FILE *fp = fopen("sample.txt", "r");
if (fp == NULL) {
perror("Error opening file");
return 1;
}
int ch = fgetc(fp);
if(ch != EOF) {
printf("First character: %c\n", ch);
} else {
printf("No character read or error occurred.\n");
}
fclose(fp);
return 0;
}
Explanation:
- The file
"sample.txt"
is opened in read mode. fgetc()
is used to read the first character from the file.- If a character is successfully read (i.e., not
EOF
), it is printed. - The file is then closed to free up resources.
Program Output:
First character: H
Example 2: Reading a File Character by Character Until End-of-File
This example illustrates how to use fgetc()
in a loop to read all characters from a file until the end-of-file is reached.
Program
#include <stdio.h>
int main() {
FILE *fp = fopen("sample.txt", "r");
if (fp == NULL) {
perror("Error opening file");
return 1;
}
int ch;
while ((ch = fgetc(fp)) != EOF) {
putchar(ch);
}
fclose(fp);
return 0;
}
Explanation:
- The file
"sample.txt"
is opened in read mode. - A loop is used to read each character using
fgetc()
untilEOF
is encountered. - Each character read is printed immediately using
putchar()
. - The file is closed after reading is complete.
Program Output:
Contents of sample.txt are printed here exactly as they appear.
Example 3: Handling End-of-File and Read Errors
This example demonstrates how to check for the end-of-file and handle potential read errors when using fgetc()
.
Program
#include <stdio.h>
int main() {
FILE *fp = fopen("sample.txt", "r");
if (fp == NULL) {
perror("Error opening file");
return 1;
}
int ch;
while (1) {
ch = fgetc(fp);
if (ch == EOF) {
if (feof(fp)) {
printf("\nEnd of file reached.\n");
}
if (ferror(fp)) {
perror("\nError reading file");
}
break;
}
putchar(ch);
}
fclose(fp);
return 0;
}
Explanation:
- The file
"sample.txt"
is opened for reading. - A loop continuously reads characters using
fgetc()
. - When
EOF
is returned, the program checks if it is due to reaching the end-of-file or a read error. - The appropriate message is printed based on the condition.
- The file is closed once the reading loop terminates.
Program Output:
(File contents printed, followed by:
End of file reached.)