C Opening and Closing Files

Intermediate
6 min read
Updated: Nov 2026
1 example

What Is Opening and Closing Files?

FILE *fopen(const char *path, const char *mode) opens a file and returns a stream, or NULL on failure. int fclose(FILE *fp) flushes pending output, closes the stream, and returns 0 on success. A program should close every file it opens, and check fclose when writing.

Remember
FILE *fp = fopen(path, "r");
if (fp == NULL) { /* report and stop */ }
/* ... use fp ... */
if (fclose(fp) != 0) { /* write error? */ }

Cheat Sheet

Keep these facts handy while you work with Opening and Closing Files in C.

TopicDetail
fopenReturns FILE * or NULL
fcloseReturns 0 or EOF
LimitOpen files per process are limited
After fcloseDo not use fp again

C Examples

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

Example 1 — Handle a missing file and a successful open

The first open fails; the second creates a new file.

C
#include <stdio.h>

int main(void) {
    FILE *fp = fopen("missing_file_123.txt", "r");
    if (fp == NULL) {
        printf("could not open missing_file_123.txt\n");
    } else {
        fclose(fp);
    }

    fp = fopen("out.txt", "w");
    if (fp == NULL) return 1;
    fputs("data\n", fp);
    if (fclose(fp) == 0) printf("closed ok\n");
    return 0;
}

How It Works

  1. Reading a file that does not exist returns NULL.
  2. Opening with "w" creates the file.
  3. fclose returning 0 means the data was flushed successfully.

Edge Cases & Pitfalls

Check these before relying on Opening and Closing Files in real programs.

leak

Leaking handles

Each early return between fopen and fclose leaks the handle. Use a cleanup section.

reuse

Using fp after fclose

The FILE * is invalid after closing.

Key Takeaways

  • Check: NULL means failure.
  • Close: every successful open.
  • Flush: fclose writes buffered data.

One line: FILE *fopen(const char *path, const char *mode) opens a file and returns a stream, or NULL on failure.

Frequently Asked Questions

NULL. The global errno holds the reason, which you can print with perror or strerror.
The OS closes files when the program exits, but you should close them explicitly to flush data, avoid leaks, and stay within the open-file limit.
Continue with "C File Modes" next. Choose r, w, a, r+, w+, a+ and the b flag. The C File Handling hub lists every topic in order.

Next: File Modes

Choose r, w, a, r+, w+, a+ and the b flag.

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.

10 people found this page helpful