Does fgets(3) really work right?

Greg Pasquariello X1190 gp at picuxa.UUCP
Wed Mar 23 22:40:55 AEST 1988


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



More information about the Comp.unix.wizards mailing list