Skip to content
Tutorials Center
My learning

Topics

C Reference ยท stdio.h

C fopen() function

Reference for C fopen(): open a file for reading, writing or appending, every mode string (r, w, a, plus, b, x), the NULL return and how to report errors.

fopen() opens the file with the given name and returns a FILE *, the handle every other file function needs. The second argument, the mode, says whether you want to read, write or append, and whether the file may be created or must already exist.

If the file cannot be opened, fopen() returns NULL and sets errno. Always check the result before using it: passing NULL to fgets() or fprintf() is undefined behavior and usually crashes.

C
#include <stdio.h>

FILE *fopen(const char *restrict path, const char *restrict mode);

Try it

C Write, append, then read

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Handling failure and exclusive creation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
FILE *fopen(const char *restrict path, const char *restrict mode);
Name Description
path The file name, relative to the current directory or absolute.
mode A string built from the letters in the table below.
Mode Meaning File must exist? Existing content
"r" read yes kept
"w" write no (created) discarded
"a" append: every write goes to the end no (created) kept
"r+" read and write yes kept
"w+" read and write no (created) discarded
"a+" read and append no (created) kept

Add b for binary mode ("rb", "wb", "r+b"; "rb+" is accepted too). Add x after w or w+ (C11, as in "wx") to make the call fail if the file already exists.

Returns: a pointer to the new stream, or NULL on failure with errno set (for example ENOENT when a file to read is missing, EACCES for missing permissions, EEXIST with x).

Errors / undefined behavior: an invalid mode string is undefined behavior. A NULL path is undefined behavior.

Usage notes

  • Check for NULL every time, and report which file failed: perror(path) prints a clear message.
  • "w" destroys the old content immediately, before you have written anything. Open with "wx" or write to a temporary name and rename() when you must not lose a file.
  • Switching between reading and writing on a + stream requires a call to fflush(), fseek(), fsetpos() or rewind() in between. Skipping it is undefined behavior.
  • Binary vs text. On Linux and macOS the b makes no difference. On Windows, text mode translates \r\n and treats Ctrl+Z as end of file, so use b for anything that is not text. Portable code always says b for binary files.
  • Close what you open. Every successful fopen() needs one fclose(). Streams left open at normal program exit are flushed and closed, but a long-running program leaks descriptors, and the number of open files is limited.
  • Paths are relative to the working directory, not to the source file. Never print absolute paths in portable examples.
  • Append mode always writes at the end, even after fseek(). Use it for logs.
  • Standard streams stdin, stdout and stderr are already open; see stdin, stdout and stderr. To reopen one of them on a file, use freopen().
  • POSIX extras: fdopen() wraps an existing file descriptor, and glibc accepts "e" (close-on-exec) and ",ccs=UTF-8" in the mode; neither is standard C.