[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

getline problems

Terry IT

11/9/2008 4:17:00 AM

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 ?
11 Answers

red floyd

11/9/2008 4:19:00 AM

0

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.

Terry IT

11/9/2008 6:06:00 AM

0

On Nov 9, 9:19 am, red floyd <no.spam.h...@example.com> wrote:
> 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.

Kai-Uwe Bux

11/9/2008 6:33:00 AM

0

Terry IT wrote:

> On Nov 9, 9:19 am, red floyd <no.spam.h...@example.com> wrote:
>> 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

Terry IT

11/9/2008 8:31:00 AM

0

On Nov 9, 11:33 am, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
> Terry IT wrote:
> > On Nov 9, 9:19 am, red floyd <no.spam.h...@example.com> wrote:
> >> 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 ?

Hendrik Schober

11/9/2008 12:13:00 PM

0

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

James Kanze

11/9/2008 1:08:00 PM

0

On Nov 9, 5:17 am, Terry IT <tryi...@gmail.com> wrote:

>  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:james.kanze@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

Daniel T.

11/9/2008 1:18:00 PM

0

Terry IT <tryitn1@gmail.com> wrote:

> 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;

ram

11/9/2008 2:35:00 PM

0

Terry IT <tryitn1@gmail.com> writes:
>i don't know whether the lastline contains a
>'\n' or it was just EOF.

This does not answer your question, but is a general comment:

The C standard (which C++ uses for basic I/O) says:

»Whether the last line[ of a text stream] requires a
terminating new-line character is implementation-defined.«
(»7.19.2 Streams«)

Because one never knows whether a program will be executed on
an implementation where a terminating new-line character is
required, I deem it to be good style to always end any
non-empty output text stream with a new-line character.

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.)

Paavo Helde

11/9/2008 4:07:00 PM

0

Terry IT <tryitn1@gmail.com> kirjutas:
> 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




James Kanze

11/10/2008 8:41:00 AM

0

On Nov 9, 9:31 am, Terry IT <tryi...@gmail.com> wrote:
> On Nov 9, 11:33 am, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
> > Terry IT wrote:
> > > On Nov 9, 9:19 am, red floyd <no.spam.h...@example.com> wrote:
> > >> 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:james.kanze@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