You are looking at lesson 11. Your place is still lesson 3, task 1: Conditions.
fopen and the NULL check, perror, the modes r/w/a, fgets and fprintf, fclose writes out the buffer, a relative path counts from the folder the program is run from.
So far the gradebook forgets everything when the program ends. For the data to stay, it has to be written to a file and read back next time.
#include <stdio.h>
FILE *f = fopen("measurements.txt", "r");
if (f == NULL) {
perror("measurements.txt");
return EXIT_FAILURE;
}
fopen returns a pointer to a FILE – a handle through which the program works with the file. If it can't open it (no such file, no permission), it returns NULL. Always check: on Windows fgets with NULL quietly reads nothing, and on Linux (and GitHub CI) the program crashes.
perror prints your text and the reason, e.g.:
measurements.txt: No such file or directory
The second argument of fopen is the mode:
| mode | what it does | if the file doesn't exist |
|---|---|---|
"r" |
read | NULL |
"w" |
write from scratch: erases the old content | creates it |
"a" |
write at the end | creates it |
The keyboard is a FILE * too: stdin. So a file is read with the same fgets as input:
char line[64];
while (fgets(line, sizeof line, f) != NULL) {
/* one line, with '\n' at the end */
}
fgets returns NULL when the file has ended (or an error happened). Writing uses fprintf – the same printf, just with a file in front:
fprintf(out, "Average: %.2f\n", average);
fprintf writes into a buffer in memory, and it reaches the disk later. fclose(f) writes out what's left and closes the file. So:
fopen – exactly one fclose;fclose returns an error (not 0) if the last part couldn't be written (e.g. the disk is full). It's the last place to see that error – check it."measurements.txt" is a relative path. It is counted from the folder the program is run from (the working folder), not from where the .c or .exe file is.
CLion's ▶ and Debug start the program in the build folder – build/debug (or cmake-build-debug). That's why the course's CMake has a helper, course_data: on Build it copies the program's data files to where the program will run. Nothing needs setting in CLion:
add_executable(p11_measurements measurements.c)
course_data(measurements.txt)
Edit the original practice/11/measurements.txt – the next Build refreshes the copy. What the program writes (e.g. report.txt) appears in the same build folder.
Each one ticks itself when you do it.
All six done. You know your way around.
It is laid out like CLion: the same panels in the same places. This tour shows each one, and you can reopen it any time with Tour.
Switches the left panel: Lesson plan, Project files, Changes. Click the active icon again to hide the panel.
In CLion: the tool window bar runs down the left edge, too.
This part is not on this task: it appears when a task needs it.
Every task in this lesson. Now marks your place. Clicking a task only lets you look; it never moves your place. Project files and Changes open here too.
In CLion: this spot holds the Project view (the file tree).
This part is not on this task: it appears when a task needs it.
Opens the file for your task by itself. A tab appears for each file the step opens. The breadcrumbs above the code show the folder.
In CLion: the same editor tabs and breadcrumbs.
This part is not on this task: it appears when a task needs it.
Diff shows what changed, like git diff: red is the old code, green the new.
In CLion: the diff view in the Commit window looks the same.
This part is not on this task: it appears when a task needs it.
Terminal for build commands, Database to see your tables. They open by themselves when a task needs them. Drag the top edge to resize.
In CLion: Terminal, and the Database tool window.
This part is not on this task: it appears when a task needs it.
What to do now, right above the code, so you never miss it. Numbered notes in the code explain the important lines. Details shows what you should see and a hint.
In CLion: no equivalent: this is your lecturer's guide.
This part is not on this task: it appears when a task needs it.
Done is the only button that moves you forward. Look back lets you review without losing your place.
This part is not on this task: it appears when a task needs it.
Guided, Focus code or Floating. You can drag the panel edges too. Your layout is remembered for next time.
In CLion: you can also rearrange and hide tool windows.
This part is not on this task: it appears when a task needs it.
Six small missions. Each one ticks itself when you do it, so you know you have found everything.