Skip to the task
Layout
Back to classic view

You are looking ahead. Your place is still task 1: fopen.

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 git diff: red = old code, green = new.
--- a/practice/11/measurements.c
+++ b/practice/11/measurements.c
@@ -1,25 +1,102 @@
+ #include <assert.h>
+ #include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_MEASUREMENTS 100
− int main(void) {
− const char *path = "measurements.txt";
+ typedef enum { OK = 0, CANNOT_OPEN, BAD_LINE, TOO_MANY, WRITE_FAILED } Error;
+
+ /* 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) {
+ 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) {
− perror(path);
− return EXIT_FAILURE;
+ return CANNOT_OPEN;
}
−
− double t[MAX_MEASUREMENTS];
− int count = 0;
+ Error result = OK;
char line[64];
− while (count < MAX_MEASUREMENTS && fgets(line, sizeof line, f) != NULL) {
− t[count] = strtod(line, NULL);
− count++;
+ while (fgets(line, sizeof line, f) != NULL) {
+ (*line_no)++;
+ char *end;
+ double x = strtod(line, &end);
+ while (isspace((unsigned char)*end)) {
+ end++;
+ }
+ if (end == line || *end != '\0') {
+ 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) {
+ 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);
+ 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];
@@ -29,17 +106,17 @@
printf("Average: %.2f\n", average);
const char *report = "report.txt";
− FILE *out = fopen(report, "w");
− if (out == NULL) {
− perror(report);
− return EXIT_FAILURE;
− }
− fprintf(out, "Measurements: %d\n", count);
− fprintf(out, "Average: %.2f\n", average);
− if (fclose(out) != 0) {
− perror(report);
+ 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