getline problems

T

Terry IT

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 ?
 
R

red floyd

Terry said:
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.
 
T

Terry IT

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

Kai-Uwe Bux

Terry said:
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
 
T

Terry IT

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 ?
 
J

James Kanze

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

Stefan Ram

Terry IT said:
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.)
 
J

James Kanze

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

James Kanze

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

adapting getline 77
about "getline" 6
Taking a stab at getline 40
TF-IDF 1
ifstream::getline() synatx 18
getline problem 10
getline buffering 8
What does fstream::getline() return after setting fail bit? 3

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top