Re: What's the guideline for dealing with unwanted chars in input stream?



Richard Heathfield wrote:
> Chuck F. said:
>
> > Richard Heathfield wrote:
> >> lovecreatesbeauty said:
> >>
> > ... big snip ...
> > >
> >>> 19
> >>> 20 /* remove unwanted chars in input stream here now */
> >>> 21 while(getchar() != '\n'); //intended null loop body
> >>
> >> This won't work, because it will keep reading and discarding
> >> characters up to AND INCLUDING the first non-newline, which is
> >> presumably supposed to be data.
> >
> > Hunh? It will remove chars from the stream, including the '\n'.
>
> Oops. My apologies. I read it as == for some reason.
>

More accurately, the "while(getchar() != '\n');" part removes every
other character in the stream so that the following "getchar()" will
only get 'even' characters.

If "Hello world\n" is in the stream then you would only be getting "el
ol\n". In this case, because there are an odd number of chars in the
stream before the "\n", the while loop will even miss the newline.

.



Relevant Pages

  • Re: Whats the guideline for dealing with unwanted chars in input stream?
    ... It will remove chars from the stream, ... then do I also need to treat the EOF character specially? ... because there are an odd number of chars in the ...
    (comp.lang.c)
  • Re: differance between binary file and ascii file
    ... Richard Heathfield wrote: ... to force-map the newline marker ... And when you are writing to the stream, ... on a Linux system. ...
    (comp.lang.c)
  • Re: Fixed
    ... Richard Heathfield wrote: ... Firefox, copied and pasted and my sorting routine worked. ... Write a C program to convert each character in a stream of "text" to hexadecimal representation. ...
    (comp.lang.c)
  • Re: Variable-sized lines of text in linked list
    ... Richard Heathfield wrote: ... text stream and be done with it. ... Their presence means your data is corrupted, ... return an error indication, just as if it got a read error. ...
    (comp.lang.c)
  • Re: How to clear stdin
    ... Richard Heathfield said: ... * the next newline character. ... If the end of the stream is ... rjh at the above domain, ...
    (comp.lang.c)