Learn › Learn C › C in Practice
valgrind - a hands-on Linux lab on a real virtual machine.
A CSV parser using struct and malloc, freed clean under valgrind.
This is the module 2 gate. Everything the module taught runs through one program: fopen from the file I/O lesson, struct and malloc from the heap lesson, and valgrind as the judge of your cleanup. Real C work looks exactly like this: read data in, build structures in memory, report, release.
Your program is called as ./solution FILE. FILE holds one user per line in the form name,uid, for example ada,1001. Parse every line into structs allocated on the heap with malloc. When the whole file has been read, print exactly one line to stdout:
N users, max uid U, longest name NAME
N is the number of records, U is the largest uid in the file, and NAME is the longest name. The max uid and the longest name usually live on different rows, so track them separately. If two names tie for longest, report the one that appears first. An empty file is not an error: print exactly 0 users and exit 0.
Two contracts carry over from the lessons. From file I/O: if the file cannot be opened, write a message containing strerror(errno) to stderr and exit 2. Not 1, and nothing on stdout. From the heap lesson: every malloc gets a matching free. One hidden test runs your binary under valgrind --leak-check=full and fails on any leak or memory error, so a correct answer that leaks is still a failing answer.
A practice file is waiting at ~/data.csv on the lab VM if you want to run the binary by hand in the terminal.
TEST compiles your code and runs the two visible sample tests. Run it as often as you like. ATTEMPT runs the full hidden suite: the missing-file exit code, the empty file, a 1000 line file, and the valgrind gate, each reported as its own test. Iterate with TEST, press ATTEMPT when you believe it is done.
Practice Lab: CSV Parser in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.