home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!usc!news.service.uci.edu!network.ucsd.edu!news!manta!mitch
- From: mitch@nosc.mil (Ray Mitchell)
- Subject: Appending To A File
- Message-ID: <1992Nov20.192627.8533@nosc.mil>
- Organization: Naval Ocean Systems Center, San Diego, CA
- Date: Fri, 20 Nov 1992 19:26:27 GMT
- Lines: 47
-
-
- Here is the crux of a test program I wrote to check the effect of
- rewinding, then attempting to write to a file opened in the "a+" mode.
- My intial expectation was that a rewind within an "a+" file would only
- rewind to the end of the data that was in the file when it was opened,
- giving an output of:
-
- First
- xxxxx
-
- However,
-
- Output on MS-DOS with both Borland and Microsoft
- First
- Second
- xxxxx
-
- Output on UNIX with both cc and gcc
- Second
- xxxxx
-
- fp = fopen("ftest", "w");
- fprintf(fp, "First\n");
- fclose(fp);
-
- fp = fopen("ftest", "a+");
- fprintf(fp, "Second\n");
-
- rewind(fp);
- fprintf(fp, "xxxxx\n");
-
- rewind(fp);
- while ((c = getc(fp)) != EOF)
- putchar(c);
-
- Although I didn't expect either output, the MS-DOS seems closer to
- what I did expect. I guess my question boils down to what the rewind
- (and I assume all the other positioning functions) are supposed to do
- in the "a+" mode. I had assumed that at least data already in the file
- when opened in that mode would be protected from overwriting but this
- doesn't seem to be the case on UNIX. Is such operation actually documented
- anywhere?
-
- Thanks,
-
- Ray Mitchell
- mitch@nosc.mil
-