Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > getline to remember location between calls

Reply
Thread Tools

getline to remember location between calls

 
 
Gary Wessle
Guest
Posts: n/a
 
      02-11-2007
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.
************************************************** **************
 
Reply With Quote
 
 
 
 
Jacek Dziedzic
Guest
Posts: n/a
 
      02-11-2007
Gary Wessle wrote:
> 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.
 
Reply With Quote
 
 
 
 
Gary Wessle
Guest
Posts: n/a
 
      02-12-2007
Jacek Dziedzic <(E-Mail Removed)> writes:

> Gary Wessle wrote:
> > 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.


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?
 
Reply With Quote
 
Jacek Dziedzic
Guest
Posts: n/a
 
      02-12-2007
Gary Wessle wrote:
> Jacek Dziedzic <(E-Mail Removed)> writes:
>
>> Gary Wessle wrote:
>>> 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.

>
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reframing a given page in a new frameset using javascript -- frames[0] goes to new location, but can't remember it? Weston Javascript 1 08-10-2007 08:34 PM
LoginCtl Remember Me Checkbox -> How to keep it checked and remember user? Frank Miverk ASP .Net 0 04-18-2007 08:13 PM
Location, location, location =?Utf-8?B?VHJhY2V5?= Wireless Networking 2 02-17-2007 08:37 PM
difference between location.href and window.location.href? saiho.yuen Javascript 3 09-14-2004 06:51 PM
Toshiba SD-4900 doesn't remember the last location Adrian DVD Video 2 04-28-2004 01:13 AM



Advertisments