C File Error Handling

Intermediate
6 min read
Updated: Nov 2026
1 example

What Is File Error Handling?

File functions report failure through return values: NULL from fopen, short counts from fread/fwrite, EOF from reads. errno records the reason, perror(msg) or strerror(errno) turns it into text, and ferror(fp) / feof(fp) tell you why a read stopped. Check results, and report clearly.

Remember
FILE *fp = fopen(path, "r");
if (!fp) {
    fprintf(stderr, "open %s: %s\n", path, strerror(errno));
    return 1;
}
if (ferror(fp)) { /* read error */ }
if (feof(fp))   { /* normal end of file */ }

Cheat Sheet

Keep these facts handy while you work with File Error Handling in C.

TopicDetail
errnoSet by the failing call; include <errno.h>
perrorPrints message plus reason to stderr
ferror / feofDistinguish error from end of file
clearerrResets indicators

C Examples

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

Example 1 — Report why a file could not be opened

The message text for ENOENT is the usual "No such file or directory".

C
#include <stdio.h>
#include <errno.h>
#include <string.h>

int main(void) {
    const char *path = "no_such_file.txt";
    FILE *fp = fopen(path, "r");

    if (!fp) {
        printf("open failed: %s\n", strerror(errno));
        return 1;
    }
    fclose(fp);
    return 0;
}

How It Works

  1. fopen returns NULL and sets errno to ENOENT.
  2. strerror(errno) converts the code to text.
  3. The message wording is defined by the C library and may differ slightly between platforms.

Edge Cases & Pitfalls

Check these before relying on File Error Handling in real programs.

errno

Reading errno too late

errno is overwritten by later calls, including printf. Save it right after the failure.

loop

Ignoring short writes

fwrite returning fewer items than requested means an error such as a full disk.

Key Takeaways

  • Check: every I/O result.
  • Explain: perror and strerror.
  • Distinguish: feof vs ferror.

One line: File functions report failure through return values: NULL from fopen, short counts from fread/fwrite, EOF from reads.

Frequently Asked Questions

Include <errno.h> and call perror("message") or print strerror(errno) immediately after the failing call.
feof tells you the end of the file was reached; ferror tells you an I/O error occurred. Both are checked after a read function reports failure.
Continue with "C Preprocessor" next. Control compilation with #include, #define, macros, conditional compilation, header guards, and predefined macros. The C File Handling hub lists every topic in order.

Next: Preprocessor

Control compilation with #include, #define, macros, conditional compilation, header guards, and predefined macros.

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