By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,500 Members | 1,769 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,500 IT Pros & Developers. It's quick & easy.

getline problems

P: n/a
hi,
i'm using code like this

string s
while(getline(cin,s)){
process(s);
}
// this is the last line
process(s);

process does some replacement and rescanning. The problem is i can't
strip or add newlines. So i don't know whether the lastline contains a
'\n' or it was just EOF.

I can't read the whole buffer as it is too huge and some line doesn't
need to be replaced.

Using while(fgets(str,MAX,stdin){
s=str
}
works but again this conversion of str to s is an overhead.

Can you suggest on how to overcome on this getline issue ?
Nov 9 '08 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Terry IT wrote:
hi,
i'm using code like this

string s
while(getline(cin,s)){
process(s);
}
// this is the last line
process(s);
This is wrong. s will not have new data after the loop.
Nov 9 '08 #2

P: n/a
On Nov 9, 9:19*am, red floyd <no.spam.h...@example.comwrote:
Terry IT wrote:
hi,
*i'm using code like this
string s
*while(getline(cin,s)){
* * process(s);
* }
// this is the last line
* process(s);

This is wrong. *s will not have new data after the loop.
i thought if file contains no newline ,then s contains all the chars
until the end of stream.
Nov 9 '08 #3

P: n/a
Terry IT wrote:
On Nov 9, 9:19*am, red floyd <no.spam.h...@example.comwrote:
>Terry IT wrote:
hi,
i'm using code like this
string s
while(getline(cin,s)){
process(s);
}
// this is the last line
process(s);

This is wrong. *s will not have new data after the loop.

i thought if file contains no newline ,then s contains all the chars
until the end of stream.
The point is not what s contains. The point is that you are processing the
last line twice. That is probably not what you want.
Best

Kai-Uwe Bux
Nov 9 '08 #4

P: n/a
On Nov 9, 11:33*am, Kai-Uwe Bux <jkherci...@gmx.netwrote:
Terry IT wrote:
On Nov 9, 9:19*am, red floyd <no.spam.h...@example.comwrote:
Terry IT wrote:
hi,
i'm using code like this
string s
while(getline(cin,s)){
process(s);
}
// this is the last line
process(s);
This is wrong. *s will not have new data after the loop.
i thought if file contains no newline ,then s contains all the chars
until the end of stream.

The point is not what s contains. The point is that you are processing the
last line twice. That is probably not what you want.

Best

Kai-Uwe Bux- Hide quoted text -

- Show quoted text -
oh! That was a mistake. if i had to read a file line by line and
output it how would i do it . if i get while(getline(cin,s)) cout
<<s<<endl;
outputs newline for everyline including the lastline. the lastline
needn't have a newline but otherlines needs to be output with '\n'.
How do i achieve it ?
Nov 9 '08 #5

P: n/a
Terry IT wrote:
hi,
i'm using code like this

string s
while(getline(cin,s)){
process(s);
}
// this is the last line
process(s);

process does some replacement and rescanning. The problem is i can't
strip or add newlines. So i don't know whether the lastline contains a
'\n' or it was just EOF.

I can't read the whole buffer as it is too huge and some line doesn't
need to be replaced.

Using while(fgets(str,MAX,stdin){
s=str
}
works but again this conversion of str to s is an overhead.

Can you suggest on how to overcome on this getline issue ?
Besides what the others pointed out:
'std::getline()' reads until the next newline (or whatever
character you passed as the optional third parameter) or
until it encounters EOF. In the latter case, IMO 'cin.eof()'
should be true.
Would that help?

Schobi
Nov 9 '08 #6

P: n/a
On Nov 9, 5:17*am, Terry IT <tryi...@gmail.comwrote:
*i'm using code like this
string s
*while(getline(cin,s)){
* * process(s);
* }
// this is the last line
* process(s);
Which was already processed in the loop.
process does some replacement and rescanning. The problem is i
can't strip or add newlines. So i don't know whether the
lastline contains a '\n' or it was just EOF.
If it doesn't end with a '\n', then it's not a text file:-).

Seriously, if you have opened the file in text mode, there is no
such thing as an incomplete line; it really depends on how your
implementation treats it.
I can't read the whole buffer as it is too huge and some line
doesn't need to be replaced.
Using while(fgets(str,MAX,stdin){
* * * * *s=str}
works but again this conversion of str to s is an overhead.
Can you suggest on how to overcome on this getline issue ?
Drop the call to process outside of the loop, and it should
work. (Supposing your implementation accepts unterminated last
lines in a text file, of course.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 9 '08 #7

P: n/a
Terry IT <tr*****@gmail.comwrote:
if i had to read a file line by line and
output it how would i do it . if i get while(getline(cin,s)) cout
<<s<<endl;
outputs newline for everyline including the lastline. the lastline
needn't have a newline but otherlines needs to be output with '\n'.
How do i achieve it ?
You have to treat either the first time getline is called special, or
the last time. It is much easier to detect which call to getline is the
first one, than which call is the last one, so treat the first one
special instead:

string s;
getline(cin, s);
cout <<< s;
while (getline(cin, s))
cout << '\n' << s;
Nov 9 '08 #8

P: n/a
Terry IT <tr*****@gmail.comkirjutas:
oh! That was a mistake. if i had to read a file line by line and
output it how would i do it . if i get while(getline(cin,s)) cout
<<s<<endl;
outputs newline for everyline including the lastline. the lastline
needn't have a newline but otherlines needs to be output with '\n'.
How do i achieve it ?
Why do you want to avoid newline after the last line? In Unix world there
is a long tradition of ending all non-empty text files with a newline. That
way you you don't get nasty surprises when you concatenate them together,
or #include them in a C/C++ file.

Paavo


Nov 9 '08 #9

P: n/a
On Nov 9, 9:31 am, Terry IT <tryi...@gmail.comwrote:
On Nov 9, 11:33 am, Kai-Uwe Bux <jkherci...@gmx.netwrote:
Terry IT wrote:
On Nov 9, 9:19 am, red floyd <no.spam.h...@example.comwrote:
>Terry IT wrote:
hi,
i'm using code like this
string s
while(getline(cin,s)){
process(s);
}
// this is the last line
process(s);
>This is wrong. s will not have new data after the loop.
I thought if file contains no newline ,then s contains all
the chars until the end of stream.
The point is not what s contains. The point is that you are
processing the last line twice. That is probably not what
you want.
oh! That was a mistake. if i had to read a file line by line and
output it how would i do it . if i get while(getline(cin,s)) cout
<<s<<endl;
outputs newline for everyline including the lastline. the
lastline needn't have a newline but otherlines needs to be
output with '\n'. How do i achieve it ?
I'm not sure what your motivation is. As I mentioned elsewhere,
it's implementation defined whether you can even write a text
file without a final newline; on most systems I've seen, you
can't. (Actually, Unix and Windows are probably about the only
ones where you can. And it doesn't have any real meaning, and
will cause all sorts of problems for other programs, under
Unix.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 10 '08 #10

P: n/a
On Nov 9, 3:35 pm, r...@zedat.fu-berlin.de (Stefan Ram) wrote:
Terry IT <tryi...@gmail.comwrites:
[...]
It might even be so that your implementation requires such a
new-line character, in which case the source text stream
would be ill-formed. (So it would not be the fault of the
code, but the duty of the producer of the stream to
terminate the last line.)
I agree, but you can't always change the producer. In such
cases, the best you can do is try to understand the file anyway
(it isn't guaranteed that you'll even see the partial line at
the end), a not perpetuate the error.

Note that in a text file, what you read and write doesn't
necessarily correspond exactly to what is on disk. If a system
adopts the convention that 0x0A is a line separator, and not a
line terminator, then the file on disk may very well not have a
trailing 0x0A---in fact, it shouldn't, since this would imply an
additional empty line. In that case, however, you still have to
write the final '\n' to the stream; it is the library code which
ensures the mapping between the internal representation of line
('\n' terminated) and the external representation (either
terminated or separated by some special character or character
sequence, or represented somehow in the organization of the file
itself).

And of course, one widespread problem is that different programs
under Windows use different conventions, so the C++ library
can't really know what to do.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 10 '08 #11

This discussion thread is closed

Replies have been disabled for this discussion.