You are looking at lesson 11. Your place is still Practice test 1, task 1: About.
A programmer's bug – assert, an error in the world – an error code. An enum with 0 = success, results through pointers, checking strtod's end, no while (!feof) and no fscanf.
Up to now, assert caught the errors. Files bring another kind of error.
average(grades, 0), NULL instead of an array. It must never happen, so assert stops the program.assert doesn't fit here: the program has to say what happened and behave sensibly.A function that can run into an error in the world returns an error code and passes its results through pointers:
typedef enum { OK = 0, CANNOT_OPEN, BAD_LINE, TOO_MANY, WRITE_FAILED } Error;
Error read_measurements(const char *path, double t[], int max, int *count, int *line_no);
An enum is a list of named integer constants: OK = 0, CANNOT_OPEN = 1, and so on. By convention 0 means "it worked". The caller always checks the code:
Error e = read_measurements(path, t, MAX_MEASUREMENTS, &count, &line_no);
if (e != OK) {
printf("%s: %s\n", path, error_text(e));
return EXIT_FAILURE;
}
From this topic on, the rule: every error code is checked. Yours and the library's: fopen (NULL), fclose (not 0), fprintf (negative).
A person may have edited the file. strtod(line, NULL) calmly returns 21 for "21,5" and tells no one. The second argument shows where the number ended:
char *end;
double x = strtod(line, &end);
while (isspace((unsigned char)*end)) {
end++;
}
if (end == line || *end != '\0') {
/* not a number, or something after it: "21,5" -> end points at ",5" */
}
end == line – there was no number at all;*end != '\0' (after skipping spaces and '\n') – junk is left after the number.Put the line number in the error message: bad.txt, line 2: not a number – that way a person finds what to fix right away.
while (!feof(f)) – feof becomes true only after a failed read, so the last line is processed twice. The loop condition is fgets(...) != NULL itself.fscanf(f, "%lf", &x) – on "21,5" it stops in the middle of the line, and it's hard to tell which line is wrong. fgets + strtod/strtol – a whole line and a clear answer.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.