In C, what is the function to open a file?
Answer
fopen
Answer
fopen
In C, the function to open a file is `fopen`.
Declared in `<stdio.h>`, `fopen` creates a stream connecting the program to a file. It accepts a pathname and a mode string such as `"r"` for reading, `"w"` for writing, or `"a"` for appending. Binary modes add `b`, as in `"rb"`.
The function returns a pointer to a `FILE` object. If opening fails—for example, because the file is missing in read mode or permissions are insufficient—it returns `NULL`, so robust programs check the result before using the stream. A successful stream should eventually be released with `fclose`.
The common mix-up is with `fread` and `fwrite`: those functions read from and write to an already-open stream. The POSIX `open` function is a lower-level system call that returns a file descriptor, whereas `fopen` belongs to C's buffered, portable stream-based I/O interface. C's file-stream design traces back to Unix and early Bell Labs C development.
Source: Wikipedia · fact-checked Aug. 2026