473,387 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

getline to remember location between calls

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.
************************************************** **************
Feb 11 '07 #1
3 2031
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.
Feb 11 '07 #2
Jacek Dziedzic <ja************************@gmail.comwrites:
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?
Feb 12 '07 #3
Gary Wessle wrote:
Jacek Dziedzic <ja************************@gmail.comwrites:
>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.
Feb 12 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Vikram | last post by:
Hi, I don't remember if it happened previously, but nowadays I'm having problem with using cin.getline function and cin>> function simultaneously. I have Visual Studio 6. If I use cin.getline...
6
by: Simon Gibson | last post by:
Hi there, im trying to write a program where you can write reports and save them into an array. im having problems with getting the string into an array tho it seems to be skipping over the...
18
by: Amadeus W. M. | last post by:
I'm trying to read a whole file as a single string, using the getline() function, as in the example below. I can't tell what I'm doing wrong. Tried g++ 3.2, 3.4 and 4.0. Thanks! #include...
1
by: Luke Wu | last post by:
I have been using the getline function from K&R2 for simple character input. The prototype is int getline(char *s, int limit); /* returns 0 to signal EOF */ This function simply calls...
6
by: Dave | last post by:
In .Net 2003 if a line, read from a text file is larger than a size parameter, the ifstream getline(buff, sze) put the file pointer to the EOF, so next peek() returns EOF. I saw this problem...
2
by: Mark P | last post by:
Consider the following snippet of code to read lines from a text file: ifstream file_stream( "some_file.txt"); string read_line; while( file_stream) { getline( file_stream, read_line); } ...
13
by: theronnightstar | last post by:
I seem to be having a problem with getline(). I have tried to find it on google, but I'm not seeing the answer. The goal of this section of code is simply to read in a line from a file to a...
20
by: Bill Waddington | last post by:
This must be a FAQ - or several FAQs - but I can't quite seem to pin it down. I need to read a string from stdin which I will then process as digits with sscanf. I need to limit the # of chars...
11
by: rory | last post by:
I am reading a binary file and I want to search it for a string. The only problem is that failbit gets set after only a few calls to getline() so it never reaches the end of the file where the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.