Thread: Streams help

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    230

    Streams help

    Been a long time since I coded but i started school today and here is my problem. Basically theres some file which called words.txt that you have to read from. You must count the number of words and then display the actual file. This is my code.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int CountTxt(ifstream&);
    void DispTxt(ifstream&);
    
    int main()
    {
        
        ifstream infile;
        infile.open("words.txt");
        
        if (!infile){
            cerr << "Error reading words.txt \n";
            exit(1);
            system("pause");
        }    
        
        cout<< "There total number of words are: " 
            << CountTxt(infile) << endl;
        
        
        infile.seekg(0);
        cout << "The text from the file was: \n\n";
        DispTxt(infile);
        
        infile.close();
        system("pause");
        return 0;
    }    
    
    int CountTxt(ifstream& in){
        string s;
       unsigned int count(0);
        
        while (in >> s){
            count++;
        }        
        return count; 
    }
    
    void DispTxt(ifstream& in){
        char c;
        in >> noskipws;
        
        while (in >> c){
          cout << c;     
        }   
        
        in >> skipws;
    }
    The problem is this: It reads the # of words fine. But it seems like it gets stuck w/ reading the file b/c afta that it doesnt display the file. But i kno my function for displayin the file works b/c i tried commenting out the first function and it displays the file the right away. Im guessing its having some difficulty with returning to the starting position... any help is appreciated.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    try seekp(), not seekg()

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    isnt seekp used with ostream. then that wouldnt work. and our instructor told us we would have to use seekg in that spot of our code.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  4. #4
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    seekg should work fine.

    "There total number of words are: "
    Should be "The total number of words is: "

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    i know it should work fine but it isnt. thats how we were told to use it and somethin in my code isnt right... help ???
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  6. #6
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    Well, seekg doesn't working properly. Try using
    Code:
    seekg(ios::end);
    seekg(ios::beg);
    Maybe telling it to seek to 0 when it's at wherever it is is screwing it up.

    Is the code after seekg running at all, or is it crashing or similar before?

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    I dont know exactly what the problem is. I'm assuming it has something to do with going through the file a second time because both functions work properly. If you test out the second fucntion alone without running the count, it displays the file with no problems. But running the code i have there my output is this.

    Code:
    The total number of words is: 17
    The text from the file was:
    It never displays the actual text from the file. It only displays it when i dont run the count function. Ive tried seekg() everyway and nothings working.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  8. #8
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    Well, I'm not at home right now so can't test it unfortunately.

    Well, could always just close then open the file again.

    Have you tried not running the count program, but still using seekg(0)? Does it still work then?

  9. #9
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    yea it works w/ out using the count.

    weve been told not to open and close b/c seekg would eliminate those steps.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  10. #10
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    I switched the order of the functions to display than count. It displays but then the count is always 0.. it seems like wateva function goes first is working correctly.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You need to clear the fail bits from the stream. The first function reads until the end of file is reached and sets an error flag. Since you are using the same stream, you must call clear() to effectively continue reading from it.

  12. #12
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Thank you. Thats exactly what i was looking for
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing to two streams: va_arg issues
    By dwks in forum C Programming
    Replies: 2
    Last Post: 09-26-2007, 10:14 AM
  2. input/output streams
    By cybernike in forum C++ Programming
    Replies: 2
    Last Post: 08-07-2007, 11:15 PM
  3. Independent streams of pseudo random number generator
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-15-2001, 05:32 AM
  4. File Streams, new error on me.
    By Eber Kain in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2001, 08:28 PM
  5. Replies: 4
    Last Post: 10-16-2001, 02:00 PM