Thread: Resetting a ifstream object after reading the whole file

  1. #1
    Registered User Zeeshan's Avatar
    Join Date
    Oct 2001
    Location
    London, United Kingdom
    Posts
    226

    Lightbulb Resetting a ifstream object after reading the whole file

    Hi,

    I am reading a simple text file as following:
    Code:
        ifstream infile("doc.kml");
        string line;
    
        while (infile >> line) {
    ........
    ........
    .......
    ......
    ........
      }
    So, after reading past the complete file; I now want to reset the ifstream object (here infile). So, that I can again start reading the file from the first line in the next part of the code. Can somebody please tell me how to do this?

    I know, I could just close the file and open it back again, but isn't there some more straightforward method for this?

    Thanks a lot in advance.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Reset infile's get pointer to the origin of the stream with seekg(). I'm fairly certain the origin is zero, but look up seekg() in a C++ reference of some sort to be sure. It ought to tell you what to do.

  3. #3
    Registered User Zeeshan's Avatar
    Join Date
    Oct 2001
    Location
    London, United Kingdom
    Posts
    226
    Thanks...what you say is also mentioned in the Reference...still Dev-C++ doesn't seem to do anything with this command!

    Anyways, thanks. I'll try this with some other compiler in university tomorrow.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You might also need to clear() the eof state.

    However, wouldn't it be possible to store the contents of the file in memory, rather than reading it many times over?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    141
    You're in luck I just recently had to solve the same darn problem myself. Here's what seemed to work for me:
    gfile is of type ifstream
    Code:
    		cerr << "Warning end of " << ifile << " reached" << endl ;
    		gfile.clear() ;
    		gfile.seekg(0, ios::beg) ;

  6. #6
    Registered User Zeeshan's Avatar
    Join Date
    Oct 2001
    Location
    London, United Kingdom
    Posts
    226
    Thanks a lot SevenThunders and anon.

    It works now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 12:21 PM
  3. Reading whole file w/ ifstream
    By cboard_member in forum C++ Programming
    Replies: 5
    Last Post: 09-03-2005, 09:09 AM
  4. Problems in reading binary file
    By serena in forum C Programming
    Replies: 3
    Last Post: 04-14-2005, 03:54 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM