🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

2 total messages Started by GNU Mailing List Maintenance Thu, 28 Dec 1995 19:30
possible bug with file input
#865
Author: GNU Mailing List Maintenance
Date: Thu, 28 Dec 1995 19:30
19 lines
755 bytes
When using getline followed by a getc for an integer,
then another getline the last getline returns a blank string. 
Is this a bug or has it already been fixed.  

Sample code:
char lname[30], rname[30];
int one;
ifstream myfile;
// and open file
myfile.getline(lname, 30);
myfile >> one;
myfile.getline(rname, 30);
...


any help is appreciated.

Thanks
Dean
Re: possible bug with file input
#866
Author: Alun Champion
Date: Thu, 28 Dec 1995 20:29
48 lines
1787 bytes
In article <gnusenet4b219e$77k@ionews.io.org> gnu@prep.ai.mit.edu (GNU Mailing List Maintenance) writes:

: When using getline followed by a getc for an integer,
: then another getline the last getline returns a blank string. 
: Is this a bug or has it already been fixed.  

: Sample code:
: char lname[30], rname[30];
: int one;
: ifstream myfile;
: // and open file
: myfile.getline(lname, 30);
: myfile >> one;
: myfile.getline(rname, 30);
: ...

getline() extracts all the character upto the '\n' and puts them in the
first argument 'lname'. It then extracts the '\n' from the stream.

So getline() on:

This is line one\n
30\n
This is line three\n

Would leave the cursor at 3.
The cin >> int would read the 3 and the 0 and leave the cursor at the
newline. When the second getline() is called it reaches a '\n' immediately
(the one on the second line) and therefore puts nothing in 'rname' and
gobbles up the '\n' leaving the cursor at 'T' on the third line.

If the data was:

This is line one\n
30This is line three\n

Then you would get what you expected.

You have to explicitly read the '\n' from the stream to get the third line
into 'rname'.

I hope all this makes sense.

Regards

   -A.
-- 
| A.Champion                |
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads