![]() |
VOOZH | about |
File handling is one of the most essential features of the C programming language, enabling programs to store, read, and manipulate data stored in files. Two of the simplest yet most commonly used functions for character-level file operations are fgetc() and fputc(). These functions allow you to read from and write to files one character at a time, making them ideal for parsing text, copying files, or performing low-level file operations.
fgetc() is used to read a single character at a time from a file.
Syntax:
int fgetc(FILE *pointer);
Parameter:
FILE* that identifies the file stream to read from.How fgetc() Works:
Important Notes About EOF:
- EOF is not a character, it is a constant defined as
-1in<stdio.h>.- The value
-1helps differentiate between valid characters (ASCII 0–255) and the "end of file" / error state.
Output:
The entire content of file is printed character by
character till end of file. It reads newline character
as well.
fputc() writes a single character to a file.
Syntax:
int fputc(int char, FILE *pointer);
Parameters:
int).FILE* to the output stream.Output:
good bye
When fputc() is executed characters of string variable are written into the file one by one. When we read the line from the file we get the same string that we entered.