Solved

going back in a text file.

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

738 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