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.
#include <stdio.h> FILE *fopen(const char *restrict path, const char *restrict mode);
Try it
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.
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
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
NULLevery 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 andrename()when you must not lose a file.- Switching between reading and writing on a
+stream requires a call tofflush(),fseek(),fsetpos()orrewind()in between. Skipping it is undefined behavior. - Binary vs text. On Linux and macOS the
bmakes no difference. On Windows, text mode translates\r\nand treats Ctrl+Z as end of file, so usebfor anything that is not text. Portable code always saysbfor binary files. - Close what you open. Every successful
fopen()needs onefclose(). 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,stdoutandstderrare already open; see stdin, stdout and stderr. To reopen one of them on a file, usefreopen(). - 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.