C/C++ Arena

Step 4 of 7

Loading records

Structured text files are everywhere: one record per line, fields separated by spaces or commas. fscanf reads fields straight into variables and returns how many it matched, which is how you detect both the end of the file and malformed lines.

while (fscanf(f, "%31s %d", p.name, &p.score) == 2) { ... }

Always give %s a width (%31s for a 32-byte buffer), so a long word can't overflow the array.

Your turn: roster.txt holds name score pairs. Load them into the array (at most 32) and print the player with the highest score, then the average score with one decimal place.

Previous: When fopen fails Next: Appending