C Notes
Complete guide to reading files in C — character-by-character with fgetc, line-by-line with fgets, formatted reading with fscanf, block reading with fread, practical examples with output.
C provides multiple ways to read from files, each suited to different situations. Need one character at a time? Use fgetc(). Want a whole line? Use fgets(). Need structured data? Use fscanf(). Working with binary? Use fread(). Let's explore each one.
File Reading Functions Overview
| Function | Reads | Best For |
|---|---|---|
fgetc() | One character | Character-by-character processing |
fgets() | One line (up to n chars) | Line-by-line text processing |
fscanf() | Formatted data | Structured text (like CSV) |
fread() | Binary block | Structs, arrays, binary data |
fgetc() — Reading One Character at a Time
int fgetc(FILE *stream);
// Returns: character read (as int), or EOF on end/errorHello World This is line 2 End of file --- Stats: 35 characters, 3 lines ---
fgets() — Reading Line by Line (Recommended)
char *fgets(char *str, int n, FILE *stream);
// Reads at most n-1 characters or until newline/EOF
// Returns: str on success, NULL on EOF/error1 | Roses are red, 2 | Violets are blue, 3 | C is amazing, 4 | And so are you!
Note: fgets() includes the newline \n in the buffer if it fits.
Removing the Trailing Newline
Name: [Alice] Name: [Bob] Name: [Charlie]
fscanf() — Formatted Reading
int fscanf(FILE *stream, const char *format, ...);
// Returns: number of items successfully read, or EOFRoll Name Marks ─────────────────────── 101 Alice 92.5 102 Bob 88.0 103 Charlie 95.3
fread() — Binary Block Reading
size_t fread(void *ptr, size_t size, size_t count, FILE *stream);
// Returns: number of items successfully readID Name Salary 1 Alice 75000.00 2 Bob 68000.00 3 Charlie 82000.00
Reading Entire File into a String
Reading CSV Data
Parsed CSV data: Name: Alice Age: 20 Score: 92.5 Name: Bob Age: 22 Score: 85.0 Name: Charlie Age: 21 Score: 91.3
Comparison: Which Function to Use?
| Scenario | Function | Why |
|---|---|---|
| Count characters | fgetc() | Process each character |
| Display file content | fgets() | Line by line is natural |
| Parse structured text | fscanf() | Format string handles parsing |
| Load binary struct | fread() | Reads exact byte count |
| Read CSV/TSV | fgets() + sscanf() | More control over line parsing |
| Read entire small file | fread() | One call, entire content |
Interview Questions
Q1: Why does fgetc() return int instead of char?
Because EOF is defined as -1 (or another negative value), which can't be represented by char on systems where char is unsigned. Returning int allows distinguishing between a valid character (0-255) and EOF (-1).
Q2: What's safer — fgets() or gets()?
fgets() is always safer because it takes a size limit parameter, preventing buffer overflow. gets() has no size limit and is so dangerous that it was removed from the C11 standard entirely. Never use gets().
Q3: What does fscanf() return?
It returns the number of items successfully read and assigned, or EOF if end-of-file is reached before any conversion. Always check the return value to detect incomplete reads.
Q4: How do you detect end-of-file correctly?
Use the return value of the reading function (NULL from fgets, EOF from fgetc, 0 from fread) rather than calling feof() before reading. The feof() function only returns true AFTER a read has already failed.
Summary
fgetc()— read one character, returns EOF when donefgets()— read a line safely (always specify buffer size)fscanf()— read formatted data (check return value!)fread()— read binary blocks of exact size- Always check return values to detect EOF and errors
- Prefer
fgets()overgets()(gets is removed from C11) - For CSV parsing, combine
fgets()withsscanf()orstrtok()
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Reading Files in C – fgetc, fgets, fscanf, fread.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, file, handling, reading, files, reading files in c – fgetc, fgets, fscanf, fread
Related C Programming Topics