home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / c / 16909 < prev    next >
Encoding:
Text File  |  1992-11-20  |  1.4 KB  |  57 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!usc!news.service.uci.edu!network.ucsd.edu!news!manta!mitch
  3. From: mitch@nosc.mil (Ray Mitchell)
  4. Subject: Appending To A File
  5. Message-ID: <1992Nov20.192627.8533@nosc.mil>
  6. Organization: Naval Ocean Systems Center, San Diego, CA
  7. Date: Fri, 20 Nov 1992 19:26:27 GMT
  8. Lines: 47
  9.  
  10.  
  11. Here is the crux of a test program I wrote to check the effect of
  12. rewinding, then attempting to write to a file opened in the "a+" mode.
  13. My intial expectation was that a rewind within an "a+" file would only
  14. rewind to the end of the data that was in the file when it was opened,
  15. giving an output of:
  16.  
  17. First
  18. xxxxx
  19.  
  20. However,
  21.  
  22.     Output on MS-DOS with both Borland and Microsoft
  23. First
  24. Second
  25. xxxxx
  26.  
  27.     Output on UNIX with both cc and gcc
  28. Second
  29. xxxxx
  30.  
  31.     fp = fopen("ftest", "w");
  32.     fprintf(fp, "First\n");
  33.     fclose(fp);
  34.  
  35.     fp = fopen("ftest", "a+");
  36.     fprintf(fp, "Second\n");
  37.  
  38.     rewind(fp);
  39.     fprintf(fp, "xxxxx\n");
  40.  
  41.     rewind(fp);
  42.     while ((c = getc(fp)) != EOF)
  43.         putchar(c);
  44.  
  45. Although I didn't expect either output, the MS-DOS seems closer to
  46. what I did expect.  I guess my question boils down to what the rewind
  47. (and I assume all the other positioning functions) are supposed to do
  48. in the "a+" mode.  I had assumed that at least data already in the file
  49. when opened in that mode would be protected from overwriting but this
  50. doesn't seem to be the case on UNIX.  Is such operation actually documented
  51. anywhere?
  52.  
  53. Thanks,
  54.  
  55. Ray Mitchell
  56. mitch@nosc.mil
  57.