On this page
fopen
Prev: fputs | Up: stdio.h | Next: fclose
Rel: stdio.h, stdio.h & File I/O Guide
Ref: cppreference — fopen
Tags: #public #prog #c #stdlib #stdio #tier/core
Include
#include <stdio.h>
Signature & Overview
FILE *fopen(const char *restrict path, const char *restrict mode);
Opens a file stream ("r", "w", "a", "rb", "wb", "wx"); returns NULL on failure and sets errno.
Notes & Pitfalls
- Always check for
NULLand pass"rb"/"wb"for binary data (required on Windows to prevent\n→\r\ntranslation).
Example
#include <assert.h>
#include <stdio.h>
int main(void) {
FILE *f = fopen("nonexistent_demo_xyz", "rb");
assert(f == NULL);
}
Linked from
Related notes
- tuts/c/stdlib/stdio/fclose Linked here; links to this page; shared tags: #c, #prog, #stdio, #stdlib, #tier
- tuts/c/stdlib/stdio/fputs Linked here; links to this page; shared tags: #c, #prog, #stdio, #stdlib, #tier
- tuts/c/stdlib/stdio Linked here; links to this page; shared tags: #c, #prog, #stdio, #stdlib
- tuts/c/stdlib/stdio_and_file_io Linked here; shared tags: #c, #prog, #stdlib
- tuts/c/stdlib/stdio/feof shared tags: #c, #prog, #stdio, #stdlib, #tier
- tuts/c/stdlib/stdio/ferror shared tags: #c, #prog, #stdio, #stdlib, #tier
- tuts/c/stdlib/stdio/fflush shared tags: #c, #prog, #stdio, #stdlib, #tier
- tuts/c/stdlib/stdio/fgets shared tags: #c, #prog, #stdio, #stdlib, #tier
- tuts/c/stdlib/stdio/fprintf shared tags: #c, #prog, #stdio, #stdlib, #tier
- tuts/c/stdlib/stdio/fread shared tags: #c, #prog, #stdio, #stdlib, #tier