C Reading Text Files

Intermediate
6 min read
Updated: Nov 2026
1 example

What Is Reading Text Files?

fgets(buf, size, fp) reads up to size - 1 characters or one line, stores a terminating '\0', and returns NULL at end of file or on error. fgetc returns one character as an int so it can also return EOF. Loop on the return value, not on feof.

Remember
char line[128];
while (fgets(line, sizeof line, fp)) {
    /* process line */
}
int ch;
while ((ch = fgetc(fp)) != EOF) { /* ... */ }

Cheat Sheet

Keep these facts handy while you work with Reading Text Files in C.

TopicDetail
fgetsSafe line reader, keeps the newline
fgetcReturns int, EOF at end
BufferAlways pass sizeof buf
EndNULL / EOF

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Writing Text Files.

Example 1 — Number the lines of a file

The program creates its own sample file first.

C
#include <stdio.h>

int main(void) {
    FILE *fp = fopen("words.txt", "w");
    if (!fp) return 1;
    fputs("alpha\nbeta\ngamma\n", fp);
    fclose(fp);

    char line[64];
    int no = 1;
    fp = fopen("words.txt", "r");
    if (!fp) return 1;
    while (fgets(line, sizeof line, fp)) {
        printf("%d: %s", no++, line);
    }
    fclose(fp);
    return 0;
}

How It Works

  1. fgets stops after each newline and keeps it in line.
  2. The loop ends when fgets returns NULL.
  3. no++ numbers the lines.

Edge Cases & Pitfalls

Check these before relying on Reading Text Files in real programs.

char

Storing fgetc in char

char c = fgetc(fp); cannot hold EOF reliably. Use int.

long

Lines longer than the buffer

A long line is split across calls. Check for a trailing newline if it matters.

Key Takeaways

  • Loop: on the return value.
  • int: for fgetc.
  • Size: pass the real buffer size.

One line: fgets(buf, size, fp) reads up to size - 1 characters or one line, stores a terminating '\0', and returns NULL at end of file or on error.

Frequently Asked Questions

Use while (fgets(buf, sizeof buf, fp)) { ... }. fgets returns NULL at end of file.
feof becomes true only after a read attempt fails, so the loop runs one extra time with stale data. Test the read function's result instead.
Continue with "C Writing Text Files" next. Write characters, strings, and formatted text with fputc, fputs, and fprintf. The C File Handling hub lists every topic in order.

Next: Writing Text Files

Write characters, strings, and formatted text with fputc, fputs, and fprintf.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful