1

So, I'm trying to read the portion between the 400 and the ***** in the following text file:

    400
    http://csweb.cs.wfu.edu
    http://college.wfu.edu/cs/wp-content/themes/wfcollegeonepro
    http://college.wfu.edu/cs/sample-page/feed
    http://college.wfu.edu/cs/wp-includes/wlwmanifest.xml
    http://college.wfu.edu/cs
    http://www
    http://www.wfu.edu
    http://college.wfu.edu
    http://college.wfu.edu/cs/news
    *****
    16331
    http://college.wfu.edu/cs/wp-content/themes/wfcollegeonepro http://csweb.cs.wfu.edu
    http://college.wfu.edu/cs/sample-page/feed http://csweb.cs.wfu.edu
    http://college.wfu.edu/cs/wp-includes/wlwmanifest.xml http://csweb.cs.wfu.edu
    http://college.wfu.edu/cs http://csweb.cs.wfu.edu
    http://www http://csweb.cs.wfu.edu
    http://www.wfu.edu http://csweb.cs.wfu.edu

I wrote the following code which (I'm pretty sure) accomplishes the task:

    file2.open("./cswebDataCopy2.txt");
    cout << "Opening cswebData.txt file..." << endl;
    if(!file2)
    {
        cout << "System failed to open cswebData.txt..." << endl;
    }
    else
    {
        cout << "cswebData.txt successfully opened!" << endl;
    }

    cout << "READING FROM: cswebData.txt" << endl;

    while(!file2.eof())
    {
        getline(file2,line4);
        //cout << line4 << endl;

        if(line4=="400")
        {
            cout << "Number of vertices in cswebData.txt: " << line4 << endl;
            continue;
        }
        else if(line3=="*****")
        {
            cout << "Found the ****** " << endl;
            break;
        }
        else
        {
            cout << "Couldn't find the 400 or ***** " << endl;
        }
    }
    cout << "Code after while loop" << endl;
    file2.close();

However, when I run the code, it only prints the code inside the else statement as many times as there are lines in the file and then the code after the while loop, even though the lines 400 and ***** are clearly in the file. So, I thought I would just print out the lines that are being read, just in-case any are being skipped. As it turns out, the program is reading all the lines correctly. Then, I thought it might have something to do with how I'm comparing in my if and else-if statements. So, I went ahead and re-wrote the code using the string compare function like so:

    while(!file2.eof())
    {
        getline(file2,line4);
        //cout << line4 << endl;

        if(line4.compare("400")==0)
        {
            cout << "Number of vertices in cswebData.txt: " << line4 << endl;
            continue;
        }
        else if(line4.compare("*****")==0)
        {
            cout << "Found the ***** " << endl;
            break;
        }
        else
        {
            cout << "Couldn't find 400 or the ***** " << endl;
        }
    }
    cout << "Code after the while loop" << endl;

However, after running the second version of my code, I still get the same (wrong) result as in my first version of the code. At this point, I am a tad bit confused on why my code isn't working. If anyone has an brilliant insight, that would be great. Thanks!

busebd12
  • 887
  • 2
  • 12
  • 23
  • 1
    This isn't your issue but don't use [`while(!file2.eof())`](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – NathanOliver Apr 23 '15 at 20:01
  • 1
    It may be that what you get back from the call to getline() has spaces in it (for example) so to compare it to "400" exactly would fail. It may be more accurate to add logic for looking for "400" as the first three characters of the line (for example). – Dean Apr 23 '15 at 20:10
  • Stupid question: "I wrote the following code which (I'm pretty sure) accomplishes the task" If it did, why post the question? :P – Steve Apr 23 '15 at 20:52
  • @NathanOliver: what should I be using instead of !file2.eof(), then? – busebd12 Apr 23 '15 at 21:17
  • You should control your loop with your read operation so if the read fails the loop is not processed. In this case you should use `while(getline(file2,line4))` – NathanOliver Apr 24 '15 at 11:58

1 Answers1

1

So, turns out that the problem was nothing to do with either versions of my code. Apparently, I had some hidden characters in my Sublime text file that were throwing off the reading of the file. When I copied and pasted the contents of the original text file into a brand new one and then ran both versions of my code on the new text file, everything worked as expected. Thanks, everyone, for y'all's input!

busebd12
  • 887
  • 2
  • 12
  • 23