elementary scanf questions

Mary Ellen Foley mef at hithere.silvlis.com
Thu Nov 23 06:03:14 AEST 1989


I have a book which says that, if you want to read in a line
at a time from standard input, you can use scanf("[^\n]\n",buffer);
Okay, you can use gets, too, and I don't know why they don't, 
unless they just want to make a point about scanf.   So....

#include <stdio.h>
main()
{
    int result;
    char buff[1024];

    do
    {
	result = scanf("%[^\n]\n",buff);
	printf("result --> %d, buff --> %s\n",result,buff);
    }
    while (result != EOF);
}


...seems to give the expected result  on an Apollo DN4000 under
sr10.1 only if I give the EOF char (^Z) twice, if I'm running it
interactively.  On the SUN (model ??) running OS 4.2 (?) (sorry, I
don't know much about SUNS),I only have to give the EOF char (^D) 
once.  Also, on both machines, control doesn't return from scanf
until the second line of input has gone in.  That is, interactively
(on SUN) your window looks like:
   foo
   bar
   result --> 1, buff --> foo
   bas
   result --> 1, buff --> bar
   (^D here, but doesn't show on the screen)
   result --> 1, buff --> bas
   result --> -1, buff --> bas


What's going on, some sort of buffering?  Where can I read about
stuff like this?  

Another book tells me that scanf is slow and compiles into a
large amount of code, but didn't say why.  Clearly, I need
a better set of books...

Please post answers, e-mail to my site has been flaky since our
sysadmin left (anybody out there want a sysadmin job?  SUN and
Apollo and DEC equipment.  Call me at 408-991-6056 for details.)

If I've overlooked something obvious, please don't flame me, just
correct me.



More information about the Comp.lang.c mailing list