getline problems

Discussion in 'C++' started by Terry IT, Nov 9, 2008.

  1. Terry IT

    Terry IT Guest

    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 ?
    Terry IT, Nov 9, 2008
    #1
    1. Advertising

  2. Terry IT

    red floyd Guest

    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.
    red floyd, Nov 9, 2008
    #2
    1. Advertising

  3. Terry IT

    Terry IT Guest

    On Nov 9, 9:19 am, red floyd <> 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.
    Terry IT, Nov 9, 2008
    #3
  4. Terry IT

    Kai-Uwe Bux Guest

    Terry IT wrote:

    > On Nov 9, 9:19 am, red floyd <> 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
    Kai-Uwe Bux, Nov 9, 2008
    #4
  5. Terry IT

    Terry IT Guest

    On Nov 9, 11:33 am, Kai-Uwe Bux <> wrote:
    > Terry IT wrote:
    > > On Nov 9, 9:19 am, red floyd <> 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 ?
    Terry IT, Nov 9, 2008
    #5
  6. Terry IT

    James Kanze Guest

    On Nov 9, 5:17 am, Terry IT <> 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:
    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
    James Kanze, Nov 9, 2008
    #6
  7. Terry IT

    Stefan Ram Guest

    Terry IT <> 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.)
    Stefan Ram, Nov 9, 2008
    #7
  8. Terry IT

    James Kanze Guest

    On Nov 9, 9:31 am, Terry IT <> wrote:
    > On Nov 9, 11:33 am, Kai-Uwe Bux <> wrote:
    > > Terry IT wrote:
    > > > On Nov 9, 9:19 am, red floyd <> 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:
    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
    James Kanze, Nov 10, 2008
    #8
  9. Terry IT

    James Kanze Guest

    On Nov 9, 3:35 pm, -berlin.de (Stefan Ram) wrote:
    > Terry IT <> writes:

    [...]
    > 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:
    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
    James Kanze, Nov 10, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John
    Replies:
    10
    Views:
    10,029
    Buster Copley
    Jul 14, 2003
  2. Jim Phelps
    Replies:
    1
    Views:
    1,714
    Karl Heinz Buchegger
    Jan 21, 2004
  3. Fred H
    Replies:
    6
    Views:
    7,384
    Kevin Goodsell
    Jan 23, 2004
  4. Simon Gibson

    cin.getline problems in case

    Simon Gibson, Feb 26, 2004, in forum: C++
    Replies:
    6
    Views:
    580
    Simon Gibson
    Feb 26, 2004
  5. Michele 'xjp'
    Replies:
    17
    Views:
    529
    James Kanze
    Jul 22, 2007
Loading...

Share This Page