Skip to the task
Layout
Back to classic view

You are looking at lesson 11. Your place is still lesson 7, task 1: Modules.

RUNTask 11 of 20 · step 4: Error codes in the programRun it again
Run it again in practice/11/

p11_measurements and ▶, as before.

Checklist

  • Every task is one commit with a clear message.
  • Zero compiler warnings: the code builds with -Werror.
  • Once per topic you step through your code in the debugger (CLion, gdb).
  • Every function has preconditions and postconditions, and assert tests check them.
  • A multi-file program: .h files with Doxygen comments and their own CMake target.
  • Input only through the safe functions of your own input.h.
  • Every error code is checked (fopen, fclose, malloc and so on).

Numbered notes in the code explain the important lines.

Details & hint
$ cd build\debug; .\practice\11\p11_measurements.exe
Measurements read: 4
Average: 21.50
Report written: report.txt
bad.txt, line 2: not a number
missing.txt: cannot open the file

You should see: The same as before, plus two errors: line 2 of bad.txt and missing.txt.

practice 11 measurements.c Code Diff +96 −19
M · MODIFIED Green = add, blue = change, red mark = removed line.
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_MEASUREMENTS 100
typedef enum { OK = 0, CANNOT_OPEN, BAD_LINE, TOO_MANY, WRITE_FAILED } Error;
Note 1: 0 – it worked, the others – what failed.
/* A description of the error code for a person. */
const char *error_text(Error e) {
switch (e) {
case OK:
return "ok";
case CANNOT_OPEN:
return "cannot open the file";
case BAD_LINE:
return "not a number";
case TOO_MANY:
return "too many measurements";
case WRITE_FAILED:
return "write failed";
}
return "unknown error";
}
/* Prints how the work with the file path ended. */
void print_error(const char *path, Error e, int line_no) {
if (e == BAD_LINE) {
printf("%s, line %d: %s\n", path, line_no, error_text(e));
} else {
printf("%s: %s\n", path, error_text(e));
}
}
/* Reads one number per line into t[0..max-1].
Before: path, t, count, line_no != NULL, max > 0.
After: OK and *count numbers; otherwise an error code,
and *line_no is the number of the line it stopped at. */
Error read_measurements(const char *path, double t[], int max, int *count, int *line_no) {
Note 2: Returns a code; the results go through pointers.
assert(path != NULL && t != NULL && count != NULL && line_no != NULL);
assert(max > 0);
*count = 0;
*line_no = 0;
FILE *f = fopen(path, "r");
if (f == NULL) {
return CANNOT_OPEN;
}
Error result = OK;
char line[64];
while (fgets(line, sizeof line, f) != NULL) {
(*line_no)++;
char *end;
double x = strtod(line, &end);
Note 3: end – where the number ended.
while (isspace((unsigned char)*end)) {
end++;
}
if (end == line || *end != '\0') {
Note 4: No number, or junk left after it.
result = BAD_LINE;
break;
}
if (*count == max) {
result = TOO_MANY;
break;
}
t[*count] = x;
(*count)++;
}
fclose(f);
return result;
}
/* Writes the report to the file path.
Before: path != NULL, count >= 0. */
Error write_report(const char *path, int count, double average) {
assert(path != NULL && count >= 0);
FILE *out = fopen(path, "w");
if (out == NULL) {
return CANNOT_OPEN;
}
int a = fprintf(out, "Measurements: %d\n", count);
int b = fprintf(out, "Average: %.2f\n", average);
if (fclose(out) != 0 || a < 0 || b < 0) {
Note 5: Both fprintf and fclose are checked.
return WRITE_FAILED;
}
return OK;
}
int main(void) {
const char *path = "measurements.txt";
double t[MAX_MEASUREMENTS];
int count;
int line_no;
Error e = read_measurements(path, t, MAX_MEASUREMENTS, &count, &line_no);
Note 6: The code is checked right away.
if (e != OK) {
print_error(path, e, line_no);
return EXIT_FAILURE;
}
double sum = 0.0;
for (int i = 0; i < count; i++) {
sum += t[i];
}
double average = count > 0 ? sum / count : 0.0;
printf("Measurements read: %d\n", count);
printf("Average: %.2f\n", average);
const char *report = "report.txt";
e = write_report(report, count, average);
if (e != OK) {
print_error(report, e, 0);
return EXIT_FAILURE;
}
printf("Report written: %s\n", report);
const char *others[] = {"bad.txt", "missing.txt"};
for (int i = 0; i < 2; i++) {
e = read_measurements(others[i], t, MAX_MEASUREMENTS, &count, &line_no);
print_error(others[i], e, line_no);
}
return EXIT_SUCCESS;
}
opened for this task
$ cd build\debug; .\practice\11\p11_measurements.exe
Measurements read: 4
Average: 21.50
Report written: report.txt
bad.txt, line 2: not a number
missing.txt: cannot open the file