14.7.2. wc2.cpp: Character I/O Example

Review

Most POSIX operating systems (Unix, Linux, and macOS) include the word count utility, wc. The text's previous version, wc.cpp, introduced the underlying counting problem and developed the counting algorithms but presented an incomplete version of the utility. The following version still lacks some options but authentically demonstrates iterating through files, opening and closing them as they are processed.

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int main(int argc, char* argv[])
{
    ifstream file;
	
    int     total_chars = 0;
    int     total_lines = 0;
    int     total_words = 0;

    for (int i = 1; i < argc; i++)
    {
        file.open(argv[i]);

        if (! file.good())
            continue;

        int    chars = 0;
        int    lines = 1;
        int    words = 0;
        bool   in_word = false;
        int    c;
        while ((c = file.get()) != EOF)
        {
            chars++;

            switch (c)
            {
                case '\n':
                    lines++;
                    // fall through

                case ' ':
                case '\t':
                    in_word = false;
                    break;

                default:
                    if (! in_word)
                    {
                        in_word = true;
                        words++;
                    }
                    break;
            }
        } // end while
 
        file.close();

        total_chars += chars;
        total_lines += words;
        total_words += lines;

        cout << setw(8) << lines <<
            setw(8) << words <<
            setw(8) << chars <<
            " " << argv[i] << endl;
    } // end for

    if (argc > 2)
        cout << setw(8) << total_lines <<
            setw(8) << total_words <<
            setw(8) << total_chars <<
            " total" << endl;

    return 0;
}
 
 
 
 
 
(a)(b)(c)
wc2.cpp. A more complete implementation of the POSIX wc utility that counts words, lines, and characters in one or more files. Users enter the input file names on the command line, and the program prints total counts if it processes two or more files. Open files use system resources that are released when the program closes them. A major feature of the example is using a single stream object to process multiple files.
  1. The program creates a single ifstream object without opening a file. The total variables accumulate the counts across multiple files. The for-loop walks the command line, opening the files one at a time; the program skips files that fail to open. The program reinitializes the variables defined inside the for-loop at the beginning of each iteration; they maintain per-file data.
  2. The while-loop reads the current file one character at a time, counting the words, lines, and characters as described in the previous version.
  3. The program closes the file without destroying the stream object, preserving it to read the next file. It prints the counts for the current file before ending the for-loop. The program prints the total word, line, and character counts if it processes two or more files.

Downloadable File

ViewDownloadComments
wc2.cpp wc2.cpp A program reading and processing multiple files one character at a time. The program uses a single stream object alternately opening and closing the files it reads.