Nearby lessons
116 of 124C - Create a File
Creating a file in C is a side effect of opening one in a write mode. Learn which modes create, which destroy, how to check whether a file already exists, and how to create files safely without losing data.
Creating With fopen
There is no separate "create" function. Open in a write mode and the file appears:
Which Modes Create a File
Every mode that can write will create a missing file. The crucial difference is what happens when the file already exists:
| Mode | If missing | If it exists | Can read? | Can write? |
|---|---|---|---|---|
"r" | Fails | Opens at the start | Yes | No |
"w" | Creates | Erases everything | No | Yes |
"a" | Creates | Keeps it, writes at the end | No | Yes |
"r+" | Fails | Opens at the start | Yes | Yes |
"w+" | Creates | Erases everything | Yes | Yes |
"a+" | Creates | Keeps it, appends | Yes | Yes |
"w" Destroys Without Warning
"w" empties it the moment fopen succeeds — before you write a single byte. There is no prompt, no error, and no undo. If your program crashes on the next line, the original contents are already gone. Any code that opens a user's file with "w" should first be sure it is meant to replace it.Check Before Creating
The portable way to avoid clobbering: try to open for reading first. If that succeeds, the file exists:
The C11 Exclusive Mode
C11 added an x suffix: create only if the file does not exist. It avoids the race window that the check-then-open approach leaves open:
Always Check for NULL
fopen returns NULL when it cannot do what you asked — a missing directory, no permission, a full disk, a bad filename. Every single fopen needs a NULL check, because writing through a NULL FILE * crashes immediately.Where the File Is Created
A bare filename lands in the program's current working directory — which is where you ran it from, not necessarily where the executable lives:
fopen Does Not Create Directories
Every folder in the path must already exist. fopen creates files, never directories:
Creating a Binary File
Add b to the mode for binary data. On Windows it stops newline translation; on Linux it changes nothing, but including it keeps the code portable:
A Complete, Safe Creation
The pattern worth memorising — check, open, verify, write, close, confirm:
Common Mistakes
- Not checking
fopenforNULL— the nextfprintfcrashes. - Using
"w"on an existing file — its contents vanish before you write anything. - Forgetting
fclose— buffered data may never reach the disk. - Expecting
fopento create directories — it will not. - Single backslashes in a Windows path —
"C:\new"contains a newline; use\\or forward slashes. - Ignoring
fclose's return value — that is where a full-disk error surfaces. - Assuming the file lands next to the executable — it lands in the working directory.
NULL check and the fclose at the same moment you write the fopen — before you write anything in between. Fill in the middle afterwards.- fopen with "w", "a", "w+", "a+" or "wb" creates the file if it does not exist.
- "w" truncates an existing file to zero bytes — data is gone instantly.
- Always check fopen for NULL before writing.
- Always fclose when finished, or buffered data may never reach disk.
- Use "r" first, or "wx" in C11, to avoid clobbering an existing file.