Thread: How do I clear ifstream!?

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    10

    How do I clear ifstream!?

    I am trying to write a simple spell checking program but no matter what I do it will only read through the word list once and all other times it just thinks it is the end of the file and does a check for the first word I put in...

    Here is the code:
    Code:
    while (repeat==true) {
            word_list.clear();
            word_list.seekg(0, ios::beg);
            cout << "\n\nType word here (lowercase): ";
            cin  >> input;
            system("clear");
                while (!word_list.eof()) {
                    word_list >> read;
                    cout << "\nTrying: " << read;
                    if (read==input) {
                        system("clear");
                        cout << " is a word!\n\n";
                        repeat=true;
                        //word_list.close();
                        word_list.clear();
                        word_list.seekg(0, ios::beg);
                        break;
                    }
                    else{
                        check0=1;
                    }
                }
                if (read!=input) {
                    system("clear");
                    //word_list.close();
                    word_list.clear();
                    word_list.seekg(0, ios::beg);
                    cout << "\n\n" << input << " is not a word!\n\n";
                }
                
            }
    EDIT: Resolved: removing word_list.close() fixed the issue
    I attached the full cpp file for reference...
    Attached Files Attached Files
    Last edited by jacobfreemaninc; 04-30-2011 at 11:26 PM. Reason: Resolved: removing word_list.close() fixed the issue

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You are actually closing the file at various places in your code (when a word is matched, or when it is not). Any subsequent operation (seekg, reading, testing eof) will indicate an error condition.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    10
    Thank you, I see that it was stupid to close the files without re-opening them. I just got rid of the close() operators and it works perfectly now. Once again thanks...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ifstream
    By Know_Your_Role in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2009, 07:05 AM
  2. ifstream
    By Wraithan in forum Tech Board
    Replies: 3
    Last Post: 09-24-2006, 01:26 AM
  3. need help with ifstream
    By stillwell in forum C++ Programming
    Replies: 6
    Last Post: 06-20-2005, 09:23 AM
  4. ifstream
    By krappykoder in forum C++ Programming
    Replies: 7
    Last Post: 12-20-2002, 11:38 AM
  5. ifstream
    By cppdude in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2002, 04:06 PM

Tags for this Thread