getline to remember location between calls

G

Gary Wessle

Hi

I have a method A which have a loop, in that loop, another method B is
called, B opens a file and does getline till found something
interesting, does something with it and return void. then the A loop
goes on till the next time B is called.

B does while( getline( ofstraam, line ) )
I am trying not to getline all over from scratch again every time B is
called but instead keep getline "ganging" in there waiting for B to be
called again.

what do I do, do I pass an open ofstream as B argument?


thank you

****************************************************************

A()
{
while ( flag )
{
/* ... */
B ( file_name, string_to_find, end_point );
}
}

B()
{
ifstream ifs( file_name.c_str() );
while( getline( ifs, line ) )
{
// does this line have the end_point then break
// look for string_to_find and do something.
}
// next time B is called with a different end_point that is after the
// previous end_point, I do not want to start from the first line.
****************************************************************
 
J

Jacek Dziedzic

Gary said:
Hi

I have a method A which have a loop, in that loop, another method B is
called, B opens a file and does getline till found something
interesting, does something with it and return void. then the A loop
goes on till the next time B is called.

B does while( getline( ofstraam, line ) )
I am trying not to getline all over from scratch again every time B is
called but instead keep getline "ganging" in there waiting for B to be
called again.

what do I do, do I pass an open ofstream as B argument?

You could (you mean an ifstream, not ofstream, right?).
Just remember that you cannot pass it by value, only by
pointer of by reference.

HTH,
- J.
 
G

Gary Wessle

Jacek Dziedzic said:
You could (you mean an ifstream, not ofstream, right?).
Just remember that you cannot pass it by value, only by
pointer of by reference.

HTH,
- J.

A()
{
ifstream ifs(file_name.c_str());
while (flag) {
B(ifs, string_to_find, end_point);
}
}

B(ifstream& r_ifs, ...) {
std::cout << r_ifs.fail() << " " << r_ifs.is_open() << std::endl;
while( getline( ifs, line ) )
{
// does this line have the end_point then break
// look for string_to_find and do something.
}
}

is printing

0 1 for the first pass
then
1 1
from then on.

why the input stream failed after the first pass, and how can I fix it?
 
J

Jacek Dziedzic

Gary said:
A()
{
ifstream ifs(file_name.c_str());
while (flag) {
B(ifs, string_to_find, end_point);
}
}

B(ifstream& r_ifs, ...) {
std::cout << r_ifs.fail() << " " << r_ifs.is_open() << std::endl;
while( getline( ifs, line ) )
{
// does this line have the end_point then break
// look for string_to_find and do something.
}
}

is printing

0 1 for the first pass
then
1 1
from then on.

why the input stream failed after the first pass, and how can I fix it?

Because
while(getline(...))
instructs the program to read until it fails. This usually means
that end-of-file is reached (the other option is a read error).

Thus, you are reading in the whole file in the first pass,
the stream goes failed after end-of-file and then the second
pass does nothing (except for printing your debug).

To make it work, your while loop in B should break upon
finding the string you are looking for. Then the whole file
will not be read (unless your string was on the last line)
and you will be able to continue in the second pass.

HTH,
- J.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,185
Latest member
GluceaReviews

Latest Threads

Top