[comp.unix.wizards] Does fgets

brad@bradley.UUCP (03/21/88)

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);
}

barmar@think.COM (Barry Margolin) (03/23/88)

In article <10900013@bradley> brad@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);
>}

Try

	printf("%s\n", str);

fgets doesn't automatically add a newline to the end if the input is
terminated because it reached your limit.  And printf doesn't output a
newline if you don't tell it to.  So, the results of the first two
fgets calls were being printed on the same line.

What I like to do when I am debugging things like this is to use more
obvious delimiters in my output, e.g.

	printf("|%s|", str);

As long as my test input doesn't contain any vertical bars, it will be
quite obvious where individual strings begin and end.  Using stuff
like this rather than newlines also results in less confusion about
how many newlines there are between things, or whether a particular
newline is at the end of one string or the beginning of the next one.

Barry Margolin
Thinking Machines Corp.

barmar@think.com
uunet!think!barmar

gwyn@brl-smoke.ARPA (Doug Gwyn ) (03/23/88)

In article <10900013@bradley> brad@bradley.UUCP writes:
>you check the output,  should we not get 15 chars on the first
>line?  Mine prints the whole thing.

It's doing just what you told it to do.  No bug.

ok@quintus.UUCP (Richard A. O'Keefe) (03/23/88)

In article <10900013@bradley>, brad@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.

gp@picuxa.UUCP (Greg Pasquariello X1190) (03/23/88)

In article <10900013@bradley>, brad@bradley.UUCP writes:
> 
> you check the output,  should we not get 15 chars on the first
> line?  Mine prints the whole thing.
> #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);
> }

Add a '\n' to your printf().  The printf is only printing the first 15 
characters, then it does the next fgets.  The following printf displays
the rest of the characters from that line in the file, but it places them
right next to the last string because no newline was encountered or supplied.

Greg Pasquariello
ihnp4!picuxa!gp

brad@bradley.UUCP (03/24/88)

Next time I will read more and open mouth later.

-thanks for those who showed me right? oh well