how do I do a 'readln' in C ??

roy schmidt rschmidt at silver.ucs.indiana.edu
Sat May 26 13:36:32 AEST 1990


In article <196 at taumet.COM> steve at taumet.UUCP (Stephen Clamage) writes:
>In article <1990May16.064747.22020 at iesd.auc.dk> kjeld at iesd.auc.dk (Kjeld Flarup) writes:
>>In article <1382 at sumax.UUCP> cadwell at sumax.UUCP (James A. Cadwell) writes:
>>>Simple question which I hope has a simple answer: how does one consume an
>>>in put line in C. Pascal wise it's 'readln'.
>>> 
>>>thank you, Jim
>>This is my simple solution. It reads until it encounters a eoln, EOF or fills
>>the buffer
>
>Well, a standard C function does precisely this, with no code to write.
>    char *fgets(char *s, int n, FILE *f);
>"s" is an array to fill, and at most n-1 characters
>will by copied from file "f", but not beyond a newline or EOF (the newline
>is in the buffer as a '\n' character).  The string in "s" is always
>terminated by a null character.  "fgets" returns null pointer on error,
>"s" otherwise.

Maybe so, but even simpler is:

     int c;

     while ((c = getchar()) != EOF && c != '\n')
          ;
     c = getchar();

And the input line is gone.  Oh, you meant you want to keep the input
line?  Arf Arf.
-----------------------------------------------------------       ^
Roy Schmidt                 |  #include <disclaimer.h>           | |
Indiana University          |  /* They are _my_ thoughts,        | |
Graduate School of Business |     and you can't have them,      <   >
Bloomington                 |     so there! */                    X
___________________________________________________________       X



More information about the Comp.lang.c mailing list