Professional C__ - Marc Gregoire [468]
Enter a file name ("STOP" to stop): STOP
That doesn’t look right. There are supposed to be five citations printed instead of five blank lines.
For this bug, you decide to try message-based debugging, and since this is a console example, you decide to print messages to cout. In this case, it makes sense to start by looking at the function that reads the citations from the file. If that doesn’t work right, then obviously the object won’t have the citations. You can modify readFile() as follows:
void ArticleCitations::readFile(const string& fileName)
{
// Code omitted for brevity
// First count the number of citations.
cout << "readFile(): counting number of citations" << endl;
while (!istr.eof()) {
string temp;
getline(istr, temp);
// Skip white space before the next entry.
istr >> ws;
cout << "Citation " << count << ": " << temp << endl;
count++;
}
cout << "Found " << count << " citations" << endl;
cout << "readFile(): reading citations" << endl;
if (count != 0) {
// Allocate an array of strings to store the citations.
mCitations = new string[count];
mNumCitations = count;
// Seek back to the start of the citations.
istr.seekg(citationsStart);
// Read each citation and store it in the new array.
for (count = 0; count < mNumCitations; count++) {
string temp;
getline(istr, temp);
cout << temp << endl;
mCitations[count] = temp;
}
}
}
Code snippet from ArticleCitations\CoutDebugging\ArticleCitations.cpp
Running the same test on this program gives the following output:
Enter a file name ("STOP" to stop): paper1.txt
readFile(): counting number of citations
Citation 0: Godel, "Uber formal unentscheidbare Satze der Principia Mathernatica und verwant der Systeme, I", Monatshefte Math. Phys., 38 (1931). 173-198.
Citation 1: Alonzo Church. "An unsolvable problem of elementary number theory", American J of Math., 58(1936), 345 363.
Citation 2: Alonzo Church. "A note on the Entscheidungsprob1em", J. of Symbolic logic, 1 (1930), 40 41.
Citation 3: Cf. Hobson, "Theory of functions of a real variable (2nd ed., 1921)", 87, 88.
Citation 4: Proc. London Math. Soc (2) 42 (1936 7), 230 265.
Found 5 citations
readFile(): reading citations
[ 5 empty lines omitted for brevity ]
Alan Turing,"On Computable Numbers with an Application to the Entscheidungsproblem", Proceedings of the London Mathematical Society, Series 2, Vol.42 (1936 - 37) pages 230 to 265.
[ 5 empty lines omitted for brevity ]
Enter a file name ("STOP" to stop): STOP
As you can see from the output, the first time the program reads the citations from the file, in order to count them, they are read correctly. However, the second time, they are not read correctly. Why not? One way to delve deeper into this issue is to add some debugging code to check the state of the file stream after each attempt to read a citation:
void printStreamState(const istream& istr)
{
if (istr.good()) {
cout << "stream state is good" << endl;
}
if (istr.bad()) {
cout << "stream state is bad" << endl;
}
if (istr.fail()) {
cout << "stream state is fail" << endl;
}
if (istr.eof()) {
cout << "stream state is eof" << endl;
}
}
void ArticleCitations::readFile(const string& fileName)
{
// Code omitted for brevity
// First count the number of citations.
cout << "readFile(): counting number of citations" << endl;
while (!istr.eof()) {
string temp;
getline(istr, temp);
// Skip white space before the next entry.
istr >> ws;
printStreamState(istr);
cout << "Citation " << count << ": " << temp << endl;
count++;
}
cout << "Found " << count << " citations" << endl;
cout << "readFile(): reading citations" << endl;
if (count != 0) {
// Allocate an array of strings to store the citations.
mCitations = new string[count];
mNumCitations = count;
// Seek back to the start of the citations.
istr.seekg(citationsStart);
// Read each citation and store it in the new array.
for (count = 0; count < mNumCitations;