Does fgets(3) really work right?
Richard A. O'Keefe
ok at quintus.UUCP
Wed Mar 23 15:56:39 AEST 1988
In article <10900013 at bradley>, brad at bradley.UUCP writes:
: A student here pointed this out to me, is this a bug? I checked
: it out on our 3B15 (V5), VAX 11/750 (4.3) and IBMPC (VENIX/86 2.0).
: you check the output, should we not get 15 chars on the first
: line? Mine prints the whole thing.
: ========cut here for file named "in"=========
: 12345678901234567890
: 12345
: aaaaa
: ========cut here for "temp.c"=============
: #include <stdio.h>
:
: main()
: {
: char str[15], *cp;
: FILE *fp;
:
: fp = fopen("in","r");
: while((cp = fgets(str, 15, fp)) != NULL)
: printf("%s", str);
: fclose(fp);
: exit(0);
: }
If you compile and run this program, the output looks exactly like "in".
But that's what it is supposed to do. To see what is happening, change
the printf() line to
printf("{%s}", str);
and when you run the program the output is
{12345678901234}{567890
}{12345
}{aaaaa
}
Better yet, read the manual page for fgets(). The whole point of using
fgets() is that you don't lose anything.
More information about the Comp.unix.wizards
mailing list