Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Restarting ifstream input

Posted on 2004-10-26
6
Medium Priority
?
946 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
Comment
Question by:Ant_Dogg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 3

Expert Comment

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

Regards
Hendrik
0
 
LVL 3

Expert Comment

by:Indrawati
ID: 12418900
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
ID: 12419029
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 3

Expert Comment

by:HendrikTYR
ID: 12419043
post some code
0
 
LVL 3

Accepted Solution

by:
Indrawati earned 200 total points
ID: 12419080
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
ID: 12422331
That was the problem exactly.  Thank you, Indrawati
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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.
Suggested Courses

636 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