Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10 5/3/83; site asgb.UUCP Path: utzoo!watmath!clyde!bonnie!akgua!sdcsvax!bmcg!asgb!gupta From: gupta@asgb.UUCP Newsgroups: net.bugs.usg Subject: Bug in USG 5.2 stdio package. Message-ID: <556@asgb.UUCP> Date: Thu, 25-Oct-84 13:47:56 EST Article-I.D.: asgb.556 Posted: Thu Oct 25 13:47:56 1984 Date-Received: Sun, 28-Oct-84 06:23:04 EST Organization: Burroughs Corporation, Boulder Colo. Lines: 66 Problem: The "fprintf(3S)" does not work when a file is fopened(3S) in "r+" mode. 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 #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 -/