CTF: File I/O
4 challenges — Week 8
File I/O Challenges
| 4 challenges | Week 8 | Week 8 (Structs, typedef, and File I/O) |
File input exercises covering reading structured and unstructured data from text files, computing aggregates, searching, and formatted output in C using FILE*, fopen, fscanf, fgets, strtok, and printf.
Difficulty Breakdown
| Level | Count |
|---|---|
| Beginner | 2 |
| Intermediate | 1 |
| Advanced | 1 |
Challenges
1. Gas Prices
| Difficulty: Beginner | Time: ~15 min | Type: function |
Write a program that reads a file gasprices.txt. The file is formatted as follows: Belgium $/gal, US $/gal, date. The program should print the average gas price over all data in the file for both countries.
Concepts: FILE* and fopen() for file reading, fscanf() with format specifiers for mixed types, accumulator pattern (running total + count), computing averages from file data, printf with %.1f formatting
2. Hours
| Difficulty: Intermediate | Time: ~20 min | Type: function |
Write a console program that prompts for the name of, and then reads, a file of data about the number of hours worked by several employees. Each line begins with the employee’s ID number, followed by their name, then a sequence of real numbers representing how many hours they worked each day. Com…
Concepts: fgets() for line-based file reading, strtok() for tokenizing strings, atof() for string-to-double conversion, variable-length data per line (unknown number of hours), accumulator pattern within a line
3. Imdb
| Difficulty: Advanced | Time: ~30 min | Type: function |
Write a program named imdb that allows the user to search the top 250 IMDB movies for a phrase and outputs movies containing that phrase and the total number of movies that match. Read data from the file imdb.txt. Each line is in the format: rank rating votes name. Search is case-insensitive.
Concepts: case-insensitive string searching (tolower + strstr), multi-function program design (decomposition), fgets for line-based file reading, strtok for parsing structured lines, strstr for substring matching
4. Weather
| Difficulty: Beginner | Time: ~12 min | Type: function |
Write a console program that reads an input file of temperatures, with real numbers representing daily high temperatures. Print the change in temperature between each pair of neighboring days.
Concepts: fencepost pattern (read first value before loop), fscanf for sequential double reading, tracking previous value for pair-wise comparisons, formatted output with %.1f, handling negative numbers in file data