Stephen Horne

← Back to blog

Published on 08/06/2024 15:10 by Stephen Horne

Deceptive Simplicity

I’ve embarked on a coding project to recreate the functionality of the Unix wc command-line tool, inspired by Code Challenges. What seemed like a straightforward task at first has proven to be an engaging learning experience.

Initially, handling command-line flags required careful consideration. One can’t make assumptions about their position or quantity within the arguments. Flags could be repeated or invalid, so they need to be parsed, handled and stored carefully.

Secondly, the program needs to handle scenarios with zero, one, or multiple file inputs. When no files are provided, it should read from standard input. With one or more files specified, standard input should be ignored.

Interestingly, the original wc tool appears to read and process a file multiple times if it’s referenced more than once in the arguments, aggregating the counts. I disagreed with this behavior, using a std::set to store the file names as they’re encountered, ensuring uniqueness.

Exploring C++‘s streams and their behavior was an enjoyable learning experience. For instance, I initially assumed that reaching the end-of-file (EOF) byte was simply another byte rather than a failure condition that flips the bad bit of the std::ifstream object. Assumptions like these are precisely why these exercises are so valuable for learning a new language.

Another valuable lesson was applying knowledge of the object lifecycle and the potential for undefined behavior when accessing memory that has been freed. While I eventually got around this issue altogether, building intuition about object lifecycles is crucial in C++.

I plan to complete this challenge soon and push the work to the repository. I’m grateful to the authors of Coding Challenges for providing these excellent exercises for free.

UPDATE: I finished the challenge: See it here. One thing I’d like to figure out is extracting all the common stream-related code, so I can use mostly the same code for ifstream and istream input to cut down on some of the repetition.

Written by Stephen Horne

← Back to blog