Writing Files in C
Writing files in C means opening a stream with fopen, writing data with functions like fputs, fprintf, or fwrite, and closing the stream with fclose. The process is the same whether you are writing text or binary data; only the mode and the function change. Mastering these basics gives you full control over persistence in C programs.
More from this site
Keep reading the latest coverage
The fopen Mode Strings
Every file operation starts with a mode. The mode controls whether you read, write, or append, and whether the file is treated as text or binary.
- "w" — Write text. Creates a new file or truncates an existing one to zero length.
- "w+" — Read and write text. Truncates the existing file.
- "a" — Append text. Creates the file if it does not exist; writes go to the end.
- "a+" — Read and append text.
- "wb", "w+b", "ab", "a+b" — The same modes in binary.
Using a text mode on Windows translates \n into \r\n on output and reverses the translation on input. Binary mode writes bytes exactly as given, which matters for any non-text data.
Writing Text with fputs and fprintf
fputs writes a string to a stream without adding a newline. fprintf formats data into the stream, much like printf but directed to a file.
FILE *fp = fopen("output.txt", "w"); if (fp == NULL) { perror("fopen"); return 1; } fputs("Hello, world\n", fp); fprintf(fp, "Value: %d\n", 42); fclose(fp);Always check the return value of fopen. If the file cannot be created or opened, fopen returns NULL and sets errno.
Writing Binary Data with fwrite
fwrite writes raw memory blocks to a file, making it the right choice for structures, arrays, and serialized data.
struct Record rec = {1, "test"}; FILE *fp = fopen("data.bin", "wb"); if (fp == NULL) { perror("fopen"); return 1; } size_t written = fwrite(&rec, sizeof(rec), 1, fp); fclose(fp);fwrite returns the number of items written. If it is less than the count you requested, the write was incomplete and you should treat it as an error.
Flushing and Error Handling
C buffers file output for performance. Data may sit in memory and not reach the disk until the buffer fills, you call fflush, or you close the file. Calling fflush(fp) forces buffered data out immediately, which is useful before a crash-prone operation or when you need to confirm the bytes are on disk.
To detect write failures, check the return value of the write function. fputs returns EOF on error; fprintf returns a negative value; fwrite returns a short item count. After fclose, you can also check its return value — it returns EOF if the close fails (which can happen when flushing final buffers).
Appending Instead of Overwriting
The "a" and "a+" modes move the write position to the end of the file before every write, so existing content is preserved. This is safer than opening in "w" mode when you do not want to lose prior data.
Common Pitfalls
- Forgetting to open the file in binary mode when writing non-text data, causing byte corruption on Windows.
- Not checking fopen for NULL, leading to a segfault on the next write.
- Assuming data is on disk until fflush or fclose is called.
- Using "w" when "a" was intended, silently truncating existing files.
Summary
Writing files in C is a three-step process: open with the correct mode, write with the appropriate function, and close or flush to ensure the data reaches the filesystem. The same pattern scales from simple log lines to large binary blobs.