Viewing Whitespace

Often students do not realize that they are mixing tabs and spaces for indentation, nor realize that they have extra whitespace at the end of the line. The reason is that it can all appear the same.

One way to do this is on a bash command line. Here is what I do for checking whitespace:

$ cat xml_parser.cpp | tr ' ' '•' | expand | tr ' ' '―' | more

The vertical bars are a pipe. In a pipe, the standard output (i.e., std::cout) of the command to the left becomes the standard input (i.e., std::cin) of the command to the right.

Command-line programs such as tr and expand are filters as they copy from standard input to standard output, performing some conversion in the process. Combining simple filters with pipes allows for flexible and complex processing. For example, if you wanted to see which lines of code have tabs:

$ cat *.hpp *.cpp | tr ' ' '•' | expand | tr ' ' '―' | grep "-"

The command line is an example of a Pipe & Filter Architecture, which we cover later in the course.