Solved

Restarting ifstream input

Posted on 2004-10-26
927 Views
Last Modified: 2011-09-20
I have an input file and I want to restart it at the end of a loop and reopen it at the beginning.

ifstream input(infile.c_str());

When I get to the end, what is the best way to start back at the top of the file?

I tried input.close(); and then ifstream input(infile.c_str()); and that does not work.

I know this is probably a really easy question but I can't seem to find the answer anywhere and its not coming to me.

Thanks

0
Question by:Ant_Dogg
    6 Comments
     
    LVL 3

    Expert Comment

    by:HendrikTYR
    input.close();
    input.open(infile.c_str());

    Regards
    Hendrik
    0
     
    LVL 3

    Expert Comment

    by:Indrawati
    There are two ways to do it:
    1. If you want to close and reopen, you can do:

    input.close();
    input.open(infile.c_str());

    2. After you reach the end of the file, you can do:

    input.seekg(0);
    or
    input.seekg(0, std::ios_base::beg);
    0
     

    Author Comment

    by:Ant_Dogg
    I tried both of those:

          input.seekg(0);

          if (input.eof( ))
          {exit(1);}

    and the program closes.  any ideas on what I'm doing wrong?  I tried the same thing with input.close and input.open.

    Thanks
    0
     
    LVL 3

    Expert Comment

    by:HendrikTYR
    post some code
    0
     
    LVL 3

    Accepted Solution

    by:
    There's nothing wrong with your code. fstream::eof() returns true whenever the stream has ever encountered end-of-file. Even if you open and close or seek the stream, ieof() will still return true if the stream has encountered end of file before. To clear it, you can call ifstream::clear().
    0
     

    Author Comment

    by:Ant_Dogg
    That was the problem exactly.  Thank you, Indrawati
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
    Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now