Intermediate · Structs, unions, enums and files · topic 34 of 60 · 30 min
Reading and writing text files
Explanation
Opening and closing
#include <stdio.h>
FILE *fp = fopen("orders.txt", "r");
if (fp == NULL) {
perror("orders.txt"); /* prints e.g. "orders.txt: No such file or directory" */
return 1;
}
/* … read … */
fclose(fp);| Mode | Meaning | If the file exists | If it does not |
|---|---|---|---|
| "r" | read | opens it | fails (NULL) |
| "w" | write | ERASES it (truncates to 0) | creates it |
| "a" | append | writes go to the end | creates it |
| "r+" | read and write | opens it | fails |
| "w+" | read and write | erases it | creates it |
| add "b" | binary mode ("rb", "wb") | no newline translation (topic 5) |
Writing text
FILE *out = fopen("bill.txt", "w");
if (!out) { perror("bill.txt"); return 1; }
fprintf(out, "%-10s %8s\n", "Item", "Amount");
fprintf(out, "%-10s %8.2f\n", "Rice", 340.0);
fputs("Thank you!\n", out);
if (fclose(out) != 0) perror("closing bill.txt"); /* fclose flushes; it can fail (disk full) */Reading line by line
char line[256];
int n = 0;
while (fgets(line, sizeof line, fp) != NULL) { /* loop on the read result */
line[strcspn(line, "\n")] = '\0';
printf("%d: %s\n", ++n, line);
}
if (ferror(fp)) perror("read error");Reading formatted data
/* marks.txt: one "name marks" pair per line, e.g. "Asha 91" */
char name[30];
int marks;
while (fscanf(fp, "%29s %d", name, &marks) == 2) { /* stop when 2 values are not read */
printf("%s scored %d\n", name, marks);
}The feof mistake
/* WRONG: processes the last line twice (or garbage once) */
while (!feof(fp)) {
fgets(line, sizeof line, fp);
puts(line);
}Standard streams
Three FILE * streams are always open: stdin (keyboard or piped input), stdout (normal output) and stderr (error messages). Print errors with fprintf(stderr, …) or perror, so they appear even when stdout is redirected to a file.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Reading and writing text files · 1/6
In this lesson you will read and write text files in C.
Prefer a video by a teacher? Find free videos on “c file handling fopen fprintf fgets fscanf fclose tutorial” on YouTube ↗
In real life: A daily attendance log
A small coaching institute's attendance program appends one line per student per day to attendance.txt in "a" mode, so earlier days are never overwritten, and a separate report program reads the file line by line to count each student's days present.
FILE *log = fopen("attendance.txt", "a");
if (!log) { perror("attendance.txt"); return 1; }
fprintf(log, "%s,%s,%s\n", "2026-10-19", "ROLL-0042", "P");
fclose(log);Practice (0/3 done)
Which mode adds lines to the end of an existing log without erasing it?
A file has 3 lines and count starts at 0. What is count after this loop? while (!feof(fp)) { if (fgets(buf, sizeof buf, fp)) count++; }
Write a program that reads a text file marks.txt of lines "name marks", prints each line with a running count, writes students with marks >= 60 into passed.txt, and prints the class average. Handle a missing input file with perror and exit code 1, and close both files.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Binary files and recordsStructs, unions, enums and files · same module
- Pointers to structs and the arrow operatorStructs, unions, enums and files · same module
- C on Linux: files and processes with system callsEmbedded C, system calls and careers
- Working with filesPython Programming · Working with data
- Reading and writing filesJava Programming (Core to Spring Boot) · Lambdas, streams and files
- Reading text: grep, less, pipes and redirectionLinux and Red Hat (RHCSA to RHCE) · Linux and the shell
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
What does fopen return when the file cannot be opened?
Answer: NULL
Check and use perror.
What does perror("x.txt") print?
Answer: "x.txt: " followed by the system's reason (from errno)
For example "No such file or directory".
Which stream should error messages go to?
Answer: stderr
stderr is not redirected with stdout.
Opening an existing important file with "w"…
Answer: Erases its contents immediately
Truncation happens at open time.
What does read return at end of file?Related
Answer: 0
0 means no more data; -1 means an error.
You open an existing file with mode "w". What happens to its old content?Related
Answer: It is erased as soon as the file opens
"w" empties the file immediately, even before you write anything.
From Working with files
Files.writeString(path, text) on an existing file, with no options…Related
Answer: Replaces the content
The default options create or truncate; use APPEND to add.
fgets returns NULL when…Related
Answer: End of input is reached (or an error occurs) before reading anything
Check it before using the buffer.
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 1 more topic in “Structs, unions, enums and files” before its exam: Binary files and records.
- Module 7 exam: Structs, unions, enums and files · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, The preprocessor, multi-file programs and make. 60 topics left in the course before the final exam.