So how do I get an ifstream to start at the top of the file again?

This is a discussion on So how do I get an ifstream to start at the top of the file again? within the C++ Programming forums, part of the General Programming Boards category; So how do I get an ifstream object to start at the top of the file again, if its already ...

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question So how do I get an ifstream to start at the top of the file again?

    So how do I get an ifstream object to start at the top of the file again, if its already read some lines with getline() and/or is already at the end of the file?
    I'm using the global getline() function which has a prototype of the following:

    Code:
    istream& getline ( istream& is, string& str );
    I tried calling ios::clear() before the getline call, but it didn't work. The ifstream object is still at the end of the file after the call to ifstream_object_name.clear().
    After attempting that, and it not working, I went and looked at the reference to ios::clear(), and learned it only clears the error flags. It doesn't reset the input filestream to the top of the file, unfortunately. So how do I do that?
    Last edited by Programmer_P; 01-23-2011 at 09:57 AM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    That's what "seekg" does: seekg - C++ Reference

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,434
    Alternately, close() then open() the stream - specifying the same filename as when the stream was opened the first time.
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by EVOEx View Post
    That's what "seekg" does: seekg - C++ Reference
    Thanks, but could you please explain in more detail how the function works (i.e. what to pass to the function, when you call it for the purpose of resetting the ifstream object to the beginning of the file)?

    I tried to follow the example at that link, and wrote:

    Code:
    input_stream2.seekg(0, ios::beg);
    but after the call to seekg(), input_stream2.eof() still returns true.

    EDIT: Wait...duh. Obviously, I'm supposed to call both seekg() AND clear().

    EDIT again: No, that didn't help anything. Its still at the end of the file apparently...
    Last edited by Programmer_P; 01-23-2011 at 08:01 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  5. #5
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by grumpy View Post
    Alternately, close() then open() the stream - specifying the same filename as when the stream was opened the first time.
    Yeah, I know. I thought of that. But, I decided against it due to the nature of things in my program.

    Thanks anyway though.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,739
    EDIT: Wait...duh. Obviously, I'm supposed to call both seekg() AND clear().
    Call them in the wrong order didya?
    Code:
            fin.clear( );
            fin.seekg( 0, std::ios::beg );
    tellg() would say 0, so you're at the beginning.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,434
    Quote Originally Posted by Programmer_P View Post
    Yeah, I know. I thought of that. But, I decided against it due to the nature of things in my program.
    What sort of things in your program would make closing and reopening a stream unsuitable?
    Right 98% of the time, and don't care about the other 3%.

  8. #8
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by whiteflags View Post
    Call them in the wrong order didya?
    Code:
            fin.clear( );
            fin.seekg( 0, std::ios::beg );
    tellg() would say 0, so you're at the beginning.
    Awesome.
    Switching the order like you said did it. My program works now.

    Thanks.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  9. #9
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by grumpy View Post
    What sort of things in your program would make closing and reopening a stream unsuitable?
    Well, I just didn't want to have to call the openFile() function more than once in my program. The openFile function open()s both filestream objects with the fileName passed to the function, then returns true if both ifstream member objects.is_open() functions returns true. The openFile(), along with findScriptTag(), findJsFunctionNames(), findHtmlElementsWithJsFunctionCall(), are all member functions of a class called C_script_operations. The ifsteam objects are also members of this class. The idea was to call findScriptTag() in int main() once for each <script> element in the input file, until the end of the file was reached through the first ifstream object. After findScriptTag() has returned, I then next call findJsFunctionNames(), passing to it the current storageStr to which findScriptTag() added the current <script> element found, from <script> to </script>. findJsFunctionNames() then searches through the storageStr to find all function names, which it then adds one at a time to a vector<string> object, which once the storageStr has been completely iterated through, is returned to the caller. Now that I have the function names in a vector<string> object, I then proceed to iterate through the vector, call findHtmlElementsWithJsFunctionCall(), passing to it the current Javascript function's name. With each call to findHtmlElementsWithJsFunctionCall(), the function has to begin with the second ifstream object pointing to the beginning of the file, because I getLine()s with it until the current line of the first ifstream object is reached. Once I reach that line with the second ifstream object, I then proceed to read every line after it until the end of the file, checking to see if the current line contains a html element which calls the current Javascript function.

    That is the basic (beginning) logic of my ExtractJavascriptFromHtmlPage program. To have done what you suggested would mean I would have to close() the C_script_operations::input_stream2 (which is the second ifstream object), and then call C_script_operations:penFile() after each call to C_script_operations::findScriptTag() and then to C_script_operations::findJsFunctionNames(), and then proceed to call C_script_operations::findHtmlElementsWithJsFunctio nCall(), and I didn't want to do that, due to the nature of my loop in int main(). I would have had to change some things, I think. So rather than rewrite code I had already written, I opted to find the better solution of starting the second ifstream object (input_stream2) at the top of the file again, if input_stream2.eof() returned true at the top of the function findHtmlElementsWithJsFunctionCall().

    EDIT: don't know why its formatting the function name like that (especially since the same name is fine for most of them)...
    There shouldn't be a space after 'o' and before 'n' in "Function", and there isn't in the editing window, so I don't know what's up.
    Last edited by Programmer_P; 01-24-2011 at 09:40 AM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  10. #10
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by whiteflags View Post
    Call them in the wrong order didya?
    Code:
            fin.clear( );
            fin.seekg( 0, std::ios::beg );
    tellg() would say 0, so you're at the beginning.
    Btw, why does the order matter anyway??
    It seems to me like it shouldn't...

    EDIT: Unless maybe seekg() sets some flags which are reset by clear() if you put the clear() call after. Yeah, that's probably what it is...
    Last edited by Programmer_P; 01-24-2011 at 09:26 AM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Programmer_P View Post
    Btw, why does the order matter anyway??
    It seems to me like it shouldn't...

    EDIT: Unless maybe seekg() sets some flags which are reset by clear() if you put the clear() call after. Yeah, that's probably what it is...
    Pretty much all the functions in I/O have a blurb that includes something like "if the failbit or badbit is set for the stream, then nothing happens". You have to put the stream back into a good state before you can do anything.

  12. #12
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by tabstop View Post
    Pretty much all the functions in I/O have a blurb that includes something like "if the failbit or badbit is set for the stream, then nothing happens". You have to put the stream back into a good state before you can do anything.
    Ahh, ok. Thanks.
    I'll make a mental note of that for future reference then, so I don't make the same mistake with I/O functions.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,434
    Quote Originally Posted by Programmer_P View Post
    That is the basic (beginning) logic of my ExtractJavascriptFromHtmlPage program. To have done what you suggested would mean <snip>
    Yeah, OK. Existing code, structured in way that is not amenable to be changed for the alternative approach I suggested.

    While I certainly don't consider there is anything wrong with the clear/seek approach, having a code structure that discourages you from using a equally simple (arguably simpler) alternative approach is usually a sign that the code will be increasingly hard to change in future. At some point, you will need to bite the bullet and structure things in a manner easier to maintain.
    Right 98% of the time, and don't care about the other 3%.

  14. #14
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by grumpy View Post
    Yeah, OK. Existing code, structured in way that is not amenable to be changed for the alternative approach I suggested.

    While I certainly don't consider there is anything wrong with the clear/seek approach, having a code structure that discourages you from using a equally simple (arguably simpler) alternative approach is usually a sign that the code will be increasingly hard to change in future. At some point, you will need to bite the bullet and structure things in a manner easier to maintain.
    Good point, and I'll make a hypothetical note of it for future reference.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by grumpy View Post
    Yeah, OK. Existing code, structured in way that is not amenable to be changed for the alternative approach I suggested.

    While I certainly don't consider there is anything wrong with the clear/seek approach, having a code structure that discourages you from using a equally simple (arguably simpler) alternative approach is usually a sign that the code will be increasingly hard to change in future. At some point, you will need to bite the bullet and structure things in a manner easier to maintain.
    I can't speak for all implementations Grumpy... but there is also a speed issue here. On windows, at least, seeking to file postion 0 is one heck of a lot faster than closing and re-opening the file.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  3. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  4. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21