Capstone: a sensor log report
Build a small streaming tool that validates readings and reports useful statistics.
- Read a stream one bounded line at a time
- Reject malformed and out-of-range records
- Aggregate valid values into a useful report
A facilities team collects temperature readings from many sensors. Their nightly job needs a quick report, even when a few log lines are corrupt. This is a realistic shape for C work: read a stream, validate each record, keep only the state you need, and send one summary to the next tool in the pipeline. The example is small; the method also appears in telemetry agents, file converters and command-line utilities.
Design before you type
Each line is sensor-id temperature, such as roomA 21.5. Accept temperatures from -100 to 200 inclusive. For every valid line, update count, total, minimum and maximum. Count invalid lines separately. When there are no valid readings, avoid dividing by zero. A fixed char line[128] makes the input limit visible; production tools should also decide how to handle lines longer than the buffer.
Try it
Up to 15 XPKeep or reject?
Classify the incoming log lines before you write the parser. What should the report count?
“roomA 21.5”
“roomB 201”
“roomC hot”
“roomD -100”
“roomE 20 extra”
1#include <math.h>
2#include <stdio.h>
3
4int main(void) {
5 char line[128];
6 while (fgets(line, sizeof line, stdin) != NULL) {
7 char name[16], extra;
8 double temperature;
9 if (sscanf(line, "%15s %lf %c", name, &temperature, &extra) != 2 ||
10 !isfinite(temperature) || temperature < -100 || temperature > 200) {
11 /* Count the invalid line and continue. */
12 continue;
13 }
14 /* Update count, total, minimum and maximum. */
15 }
16 return 0;
17}Use fgets to read a bounded line, then sscanf with a width on the name and an extra conversion to catch trailing non-whitespace text. isfinite rejects NaN and infinity. This exercise expects short log lines; in a production parser, detect and drain overlong lines before processing the next record. Compile locally with gcc -std=c17 -Wall -Wextra -Wpedantic -g -fsanitize=address,undefined report.c -lm while testing. The Clang AddressSanitizer and UndefinedBehaviorSanitizer guides explain what those tools catch.
Key takeaways
Bound input, validate each record and carry only necessary state.
Treat an empty valid set as a real case.
Use warnings, sanitizers and malformed inputs before trusting a command-line tool.
Lesson quiz
4 questions · pass with 3 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Practice: write C
These run for real. Write the program, press Run tests, and read what the compiler tells you - learning to read compiler messages is half of learning C.
Build the streaming report
Read lines of sensor-id temperature from stdin. Accept temperatures from -100 to 200 inclusive. Reject malformed lines and trailing text. Print samples=N average=X.X min=X.X max=X.X invalid=M. If no valid samples exist, print samples=0 invalid=M. Keep the parser bounded and use isfinite.
- Mixed log
- No valid records
- Boundary and trailing text
Your code is compiled with gcc 14 (-Wall -Wextra) and run on Compiler Explorer (godbolt.org), a free public service.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…