This page last updated on: Feb. 25, 2002
The command line argument handling includes a required document file argument, while the main dictionary file argument is optional (defaulted to defdict.txt if not entered), and the local dictionary file is optional (see the syllabus for more information).
The application file parsing functionality will scan through the document and dictionary files, parsing each line for either dictionary or spell checking functionality.
spellchk DocFile MainDictFile LocalDictFileThe document file is required, the main dictionary file is optional (use a default file if not specified), and the local dictionary file is also optional (if not specified, no local dictionary will be used). (A useful enhancement is to add a help option, which displays the command syntax and options to the user.) Handle all appropriate combinations.
The dictionary files contain multiple text lines, with each line containing one word. The document file can contain any kind of text and punctuation characters.
The main function consists of the following functionality:
Name the source file spellchk.cpp. If the file parsing functionality is put into separate source files, name them parse.cpp and parse.h. The complete application consists of these source files plus the source files for the Word and Dictionary classes.
char const * localDictFname = 0; char const * mainDictFname = "defdict.txt"; char const * docFname = 0;Set the pointers to the appropriate argv entry.
Since much of the file parsing is similar, a function can be defined to perform it, with a prototype similar to:
#include <vector> // include std C++ library vector container class bool parseFile ( char const * fName, std::vector<Word>& v );The return value will indicate success or failure.
The parsing function will be passed a file name and an empty std::vector container object as parameters, and will insert Word objects into the container as they are parsed from the file. The function will perform appropriate error handling, including file open and read errors.
Inside the parseFile function, each word needs to be scanned and located correctly so that a Word object can be built and appended to the container. Since the Word class handles non-alphabetic characters and case sensitivity, it should be sufficient to locate the end of the text word in the input buffer and delimit it appropriately (with a null character) before passing it to the Word constructor. Use standard C++ I/O functions for file handling, using the following include files:
#include <iostream> #include <fstream>An I/O object of type std::ifstream can be created, passing in the name of the file as the constructor argument:
std::ifstream inFile(fname);Errors or end-of-file can be checked by invoking a conversion operator:
if (inFile) { // inFile object is in a good state
The simplest approach to parsing the file is to use the std::fstream
operator>> functionality to fill up a standard C++ library std::string
object. As each std::string object is extracted, it will contain
characters from the std::fstream up to a whitespace character:
#include <string> // C++ std lib string class
std::string wordStr;
while (inFile >> wordStr) { // do something with the string object
If there are one or more alphabetic characters, a Word object
can then be constructed, relying on the Word constructor to skip
the non-alpha characters. A std::string object provides read access
to the internal data as a char const * with the c_str()
member function (e.g. wordStr.c_str() ).
Explanations and more detail will be provided in class, if necessary.