Article View: gnu.g++.help
Article #866Re: possible bug with file input
From: Alun Champion
Date: Thu, 28 Dec 1995 20:29
Date: Thu, 28 Dec 1995 20:29
48 lines
1787 bytes
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 |
Technical Details
Message-ID:
<ALUN.CHAMPION.95Dec28152915@g7240065.bridge.bst.bls.com>Path:
arc1-ca.pugleaf.net!atglab.bls.com!Alun.ChampionReferences:
<gnusenet4b219e$77k@ionews.io.org>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!atglab.bls.com!Alun.Champion From: Alun.Champion@bridge.bst.bls.com (Alun Champion) Newsgroups: gnu.g++.help,comp.lang.c++ Subject: Re: possible bug with file input Date: 28 Dec 1995 20:29:15 GMT Organization: Computer People Inc. Distribution: world Message-ID: <ALUN.CHAMPION.95Dec28152915@g7240065.bridge.bst.bls.com> References: <gnusenet4b219e$77k@ionews.io.org> NNTP-Posting-Host: bstfirewall.bst.bls.com In-reply-to: gnu@prep.ai.mit.edu's message of 28 Dec 1995 14:30:01 -0500