Skip to the task
Layout
Back to classic view

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

HOMEWORKTask 19 of 20 · step 7: Homework: the gradebook in a fileHomework: the gradebook in a file
Homework: the gradebook in a file in practice/11/

hw/11-gradebook: error_text, save_students and load_students with error codes; main loads gradebook.txt, adds students and saves; it doesn't overwrite a broken file.

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).
Details & hint

The gradebook, version 5: students no longer disappear. At the start the program loads gradebook.txt, lets you add new ones, and at the end saves everything.

The task: hw/11-gradebook

input.h, input.c – put your own from hw/10-gradebook here.

gradebook.h – replace the first part with yours from hw/10-gradebook. The new part is written: GRADEBOOK_FILE ("gradebook.txt"), Error and three functions with contracts.

typedef enum {
    GB_OK = 0,
    GB_CANNOT_OPEN,
    GB_BAD_FORMAT,
    GB_TOO_MANY,
    GB_WRITE_FAILED
} Error;

gradebook.c – copy your functions from hw/10-gradebook and write the new ones:

function what it does
error_text(e) ok, cannot open the file, bad line format, too many students, write failed
save_students(path, s, n) each student is a line Ona Petraite: 8 10 7; checks fopen, fprintf, fclose
load_students(path, s, max, &n, &line_no) reads lines with fgets; a bad line → GB_BAD_FORMAT and its number line_no; more than max → GB_TOO_MANY

A good line: the name up to the first : (is_name returns 1, it fits in MAX_NAME), after it 1 to MAX_GRADES grades from 1 to 10, separated by spaces. An empty line, 8,5, 11, x – a bad format. An empty file – GB_OK and 0 students.

main.c:

Students loaded from gradebook.txt: 2
How many new students? 1
Student 1 of 1
Full name: Agne Kazlauskaite
...
Students by name:
Agne Kazlauskaite (A. K.), average 9.00: 9 9
Jonas Jonaitis (J. J.), average 6.50: 6 7
Ona Petraite (O. P.), average 8.33: 8 10 7
Best average: Agne Kazlauskaite (9.00)
Students saved to gradebook.txt: 3
  • No file (GB_CANNOT_OPEN) – No gradebook.txt yet: starting an empty gradebook. and carry on as with 0 students.
  • Any other error – Error: gradebook.txt, line 2: bad line format., The gradebook is unchanged. and return EXIT_FAILURE;: don't overwrite a broken file.
  • New students – from 0 to the free places (New students must be between 0 and 8.); when there are none – The gradebook is full.
  • With 0 students there is no Best average line.
  • If save_students fails – Error: gradebook.txt: write failed. and EXIT_FAILURE.

gradebook.txt appears in the folder the program runs in: with ▶, the build folder (build/debug or cmake-build-debug).

Tests

ctest --preset debug -L hw11
  • hw11_functions – unit_tests.c: save and load, an empty file, no file, ten bad lines (each one on line 2), too many students. While the functions are empty:

    Assertion failed: nearly_equal(average(grades, 5), 8.8), file .../hw/11-gradebook/unit_tests.c, line 36
    
  • hw11_precondition – save_students(..., -1) must stop at your assert.

  • hw11_gradebook_1–3 – the whole program, each in its own folder: no file; a file with two students and invalid numbers; a broken file. Both the output and what was written to gradebook.txt are checked (in test 3 – that it is unchanged).

  • hw11_input, hw11_doxygen, hw11_errors, hw11_main – input.c is yours, gradebook.h has Doxygen and Error, gradebook.c uses assert(, fopen(, fclose(, fgets( and doesn't use fscanf( or while (!feof(, main.c calls load_students(, save_students(, error_text( and checks GB_CANNOT_OPEN; no TODO anywhere.

Handing in

Commit and push. Actions → Points: HW 11 (Gradebook with a file), 10 points when all 9 tests pass.

practice 11 measurements.c Code Diff
UNCHANGED This step does not change this file.
#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;
/* 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) {
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);
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];
}
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;
}