Bug in USG 5.2 stdio package.

gupta at asgb.UUCP gupta at asgb.UUCP
Fri Oct 26 04:47:56 AEST 1984


Description:  If a file is opened [using fopen(3S)] in the "r+" mode, read till
	the end of file is reached, and then written to using "fprintf(3S)",
	the call returns the correct number of characters written to the file
	but does NOT append to the file! To recreate the effects, try the 
	program given below.

Environment:  Vax-11/780*, running Unix* 5.2

Solution:  ??? (Use sprintf and fputs instead :-)

/* -------  Program to create the problem:  -------- */
/*
	Bug in stdio routines.
	This program finds out the number of lines (assumes line length < LEN)
	in a file and then appends line to the file containing the count.

	step 1.  Compile with code segment 1 and test.
		 It works.
	step 2.  Compile with code segment 2 and test.
		 It doesn't work!
	PROGRAM USES fopen.test AS TEST FILE. fopen.test SHOULD EXIST.
*/

#include  <stdio.h>

#define   INF  "fopen.test"
#define   LEN  80

main()
{
	FILE *fp;
	int  j = 0;
	char line[LEN], *p;
	
	fp = fopen(INF, "r+");     /* Open file for read and update */
	while ((p=fgets(line,LEN,fp)) != NULL) /* find num. of lines in file */
		j++;

	/* Begin segment 1 -- Write the number of lines to string and then
			      write string to file   */

	sprintf(line,"%d\n",j); 
	j = fputs(line,fp);

	/* End segment 1 */

	/* Begin segment 2 -- Write number of lines to file

	j = fprintf(fp,"%d\n",j);

	-- End segment 2 */

	fprintf(stdout, "Number of chars written: %d\n",j); /* print number of
							    chars written */
	fclose(fp);   		    /* Close file */
	exit(0);
}

/* -------------------------  End of program  ------------------ */

* Vax-11/780 is a trademark of Digital Equip. Corp.
  Unix is a trademark of Bell Labs.

Yogesh Gupta
sdcrdcf -- bmcg!asgb!gupta
sdcsvax -/



More information about the Net.bugs.usg mailing list