C File Handling

Intermediate
16 min read
Updated: Nov 2026
1 example
10 guides

Definition and Usage

File handling lets a program store data permanently. C works with files through streams represented by FILE * pointers declared in <stdio.h>. The workflow is always open, use, close: fopen returns a FILE *, functions such as fprintf, fgets, fread, and fwrite move data, and fclose flushes and releases the file. This section covers modes, text and formatted I/O, binary files, random access with fseek, and error handling.

Always check fopen.

fopen returns NULL when a file cannot be opened. Check it before every read or write, and call fclose for every successful open.

Learning Path

Work through the stages in order, or jump to a topic from the index below (10 tutorials).

C Examples

A quick taste of file handling. Compile with gcc file.c -std=c11 -Wall -o out.

Example 1 — Write a file, then read it back

Open for writing, close, reopen for reading.

C
#include <stdio.h>

int main(void) {
    FILE *fp = fopen("demo.txt", "w");
    if (!fp) return 1;
    fprintf(fp, "Hello, file!\n");
    fclose(fp);

    char line[64];
    fp = fopen("demo.txt", "r");
    if (!fp) return 1;
    if (fgets(line, sizeof line, fp)) printf("%s", line);
    fclose(fp);
    return 0;
}

How It Works

  1. Mode "w" creates or truncates demo.txt.
  2. fprintf writes formatted text; fclose flushes it.
  3. fgets reads one line, including the newline.

C File Handling Topics

Every guide in this section with a one-line summary (10 tutorials).

Open and close

4 tutorials

FILE pointers, fopen, fclose, and mode strings.

TopicDescriptionTutorial
File handling basicsUnderstand streams, the open-use-close workflow, and text versus binary files.Try it
File pointersUse FILE * to represent streams, including stdin, stdout, and stderr.Try it
Opening and closing filesCall fopen, test for NULL, and always fclose.Try it
File modesChoose r, w, a, r+, w+, a+ and the b flag.Try it

Read and write

4 tutorials

Text, formatted, and binary file I/O.

TopicDescriptionTutorial
Reading text filesRead lines and characters with fgets and fgetc.Try it
Writing text filesWrite characters, strings, and formatted text with fputc, fputs, and fprintf.Try it
Formatted file I/OWrite and parse records with fprintf and fscanf.Try it
Binary filesStore raw data with fwrite and fread in "rb" and "wb" modes.Try it

Positioning and errors

2 tutorials

Seek around a file and handle failures.

TopicDescriptionTutorial
Random file accessJump around a file with fseek, ftell, and rewind.Try it
File error handlingDetect and report failures with errno, perror, ferror, and feof.Try it

Edge Cases & Pitfalls

Common mistakes when learning file handling in C.

null

Unchecked fopen

A missing file or wrong permission returns NULL; using it crashes.

close

Forgetting fclose

Buffered output may never reach disk and file handles leak.

eof

Looping on feof

while (!feof(fp)) runs one extra iteration. Test the read function's return value instead.

Key Takeaways

  • Workflow: open, use, close.
  • Check: every open and every read.
  • Binary vs text: choose the mode that fits the data.

One line: File handling lets a program store data permanently.

Frequently Asked Questions

FILE * points to a stream object managed by the C library that tracks the file, its buffer, and the current position.
Text files store characters and may translate newlines; binary files store raw bytes exactly as written.
fclose flushes buffered data to disk and releases the resources used by the stream.

Next: File Handling Basics

Understand streams, the open-use-close workflow, and text versus binary files.

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.

9 people found this page helpful