Solved

going back in a text file.

Posted on 2002-07-24
2
175 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Suggested Solutions

Title # Comments Views Activity
convert char array to number in c 5 86
How to convert MFC APP to Win32 APP. 19 97
Embarcadero C++ Builder XE10.1 Berlin red arrow Indicator 2 54
Outlook 13 43
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

830 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