Solved

going back in a text file.

Posted on 2002-07-24
2
173 Views
Last Modified: 2010-04-01
I am opening a file
ifstream data ("data.txt");

and then reading some details from it.
while (getline(data, line, '\n'))
{
data_vec.push_back(line);
}

I would then like to go back to the start of the file so that I can read through it again.  
I am trying to use data.seekg(0, ios::beg) but when I run data.getline() it still only grabs the last entry in the file.  
How can I go back to the start of the file.  Do I need to close the file and then open it again?
0
Comment
Question by:Karls
2 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 7176236
Try data.seekg(1, ios::beg)
0
 
LVL 8

Accepted Solution

by:
mnashadka earned 50 total points
ID: 7176288
If you hit the end of the file, you need to call data.clear(); in order to clear the eof flag.  Then do data.seekg(0, ios::beg);
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

932 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

10 Experts available now in Live!

Get 1:1 Help Now