fscanf can parse simple structured records from a file.

record fields Each successful scan fills the fields for one record.
running total The loop accumulates one field from each parsed record.

Parse Records

bonus
parse_records.c
Replay: real traced execution (multi-file project)
#include <stdio.h>

int main(void) {
    int bonus = 0;
    FILE *file = fopen("parse_records_demo.txt", "w+");

    if (file == 0) {
        return 1;
    }

    fprintf(file, "A %d\n", 10 + bonus);
    fprintf(file, "B %d\n", 20);
    rewind(file);

    char name = 0;
    int score = 0;
    int total = 0;

    while (fscanf(file, " %c %d", &name, &score) == 2) {
        total += score;
    }

    fclose(file);
    remove("parse_records_demo.txt");

    printf("total=%d\n", total);
    return 0;
}
#include <stdio.h>

int main(void) {
    int bonus = 5;
    FILE *file = fopen("parse_records_demo.txt", "w+");

    if (file == 0) {
        return 1;
    }

    fprintf(file, "A %d\n", 10 + bonus);
    fprintf(file, "B %d\n", 20);
    rewind(file);

    char name = 0;
    int score = 0;
    int total = 0;

    while (fscanf(file, " %c %d", &name, &score) == 2) {
        total += score;
    }

    fclose(file);
    remove("parse_records_demo.txt");

    printf("total=%d\n", total);
    return 0;
}
#include <stdio.h>

int main(void) {
    int bonus = 10;
    FILE *file = fopen("parse_records_demo.txt", "w+");

    if (file == 0) {
        return 1;
    }

    fprintf(file, "A %d\n", 10 + bonus);
    fprintf(file, "B %d\n", 20);
    rewind(file);

    char name = 0;
    int score = 0;
    int total = 0;

    while (fscanf(file, " %c %d", &name, &score) == 2) {
        total += score;
    }

    fclose(file);
    remove("parse_records_demo.txt");

    printf("total=%d\n", total);
    return 0;
}
  1. bonus ← 0, file ← ⟨addr A⟩, name ← , score ← 0, total ← 0

    3int main(void) {4    int bonus→ 0 = 0; //@bonus=5, 105    FILE *file→ ⟨addr A⟩ = fopen("parse_records_demo.txt", "w+");67    if (file == 0) {8        return 1;9    }1011    fprintf(file⟨addr A⟩, "A %d\n", 10 + bonus0);12    fprintf(file⟨addr A⟩, "B %d\n", 20);13    rewind(file⟨addr A⟩);1415    char name = 0;16    int score→ 0 = 0;17    int total→ 0 = 0;
  2. total ← 10

    pass 1 of 2
    19while (fscanf(file⟨addr A⟩, " %c %d", &nameA, &score10) == 2) {20    total→ 10 += score10;21}
  3. total ← 30

    pass 2 of 2
    19while (fscanf(file⟨addr A⟩, " %c %d", &nameB, &score20) == 2) {20    total→ 30 += score20;21}
  4. fclose(file);

    23    fclose(file⟨addr A⟩);24    remove("parse_records_demo.txt");2526    printf("total=%d\n", total30);27    return 0;28}
    outputtotal=30
  1. bonus ← 5, file ← ⟨addr A⟩, name ← , score ← 0, total ← 0

    3int main(void) {4    int bonus→ 5 = 5;5    FILE *file→ ⟨addr A⟩ = fopen("parse_records_demo.txt", "w+");67    if (file == 0) {8        return 1;9    }1011    fprintf(file⟨addr A⟩, "A %d\n", 10 + bonus5);12    fprintf(file⟨addr A⟩, "B %d\n", 20);13    rewind(file⟨addr A⟩);1415    char name = 0;16    int score→ 0 = 0;17    int total→ 0 = 0;
  2. total ← 15

    pass 1 of 2
    19while (fscanf(file⟨addr A⟩, " %c %d", &nameA, &score15) == 2) {20    total→ 15 += score15;21}
  3. total ← 35

    pass 2 of 2
    19while (fscanf(file⟨addr A⟩, " %c %d", &nameB, &score20) == 2) {20    total→ 35 += score20;21}
  4. fclose(file);

    23    fclose(file⟨addr A⟩);24    remove("parse_records_demo.txt");2526    printf("total=%d\n", total35);27    return 0;28}
    outputtotal=35
  1. bonus ← 10, file ← ⟨addr A⟩, name ← , score ← 0, total ← 0

    3int main(void) {4    int bonus→ 10 = 10;5    FILE *file→ ⟨addr A⟩ = fopen("parse_records_demo.txt", "w+");67    if (file == 0) {8        return 1;9    }1011    fprintf(file⟨addr A⟩, "A %d\n", 10 + bonus10);12    fprintf(file⟨addr A⟩, "B %d\n", 20);13    rewind(file⟨addr A⟩);1415    char name = 0;16    int score→ 0 = 0;17    int total→ 0 = 0;
  2. total ← 20

    pass 1 of 2
    19while (fscanf(file⟨addr A⟩, " %c %d", &nameA, &score20) == 2) {20    total→ 20 += score20;21}
  3. total ← 40

    pass 2 of 2
    19while (fscanf(file⟨addr A⟩, " %c %d", &nameB, &score20) == 2) {20    total→ 40 += score20;21}
  4. fclose(file);

    23    fclose(file⟨addr A⟩);24    remove("parse_records_demo.txt");2526    printf("total=%d\n", total40);27    return 0;28}
    outputtotal=40