home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / c / 13400 < prev    next >
Encoding:
Text File  |  1992-09-09  |  1.6 KB  |  34 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!spool.mu.edu!agate!dog.ee.lbl.gov!porpoise!manta!mitch
  3. From: mitch@nosc.mil (Ray Mitchell)
  4. Subject: File Handling
  5. Message-ID: <1992Sep9.173717.22248@nosc.mil>
  6. Organization: Naval Ocean Systems Center, San Diego, CA
  7. Date: Wed, 9 Sep 1992 17:37:17 GMT
  8. Lines: 24
  9.  
  10.  
  11. This problem must have an easy solution.  Assume the typical case where a
  12. binary file consists of several records (structures).  I wish to write a
  13. program in which the user specifies which record is to be modified.  My
  14. initial approach was to simply fseek into the file the appropriate amount
  15. then overwrite the record at that position.  That worked fine.
  16.  
  17. The problem comes when I attempt to do some error checking.  If the user
  18. specifies a record which is not in the file and fseek beyond the end of
  19. file is generated.  I had hoped that such an fseek would return an error
  20. but apparently fseek blindly sets the file position indicator in the
  21. FILE structure without testing if the seek is possible.  My next approach
  22. was to ignore the return value of fseek and instead call ftell immediately
  23. after fseek and compare the return from ftell with the offset I had
  24. specified in fseek.  No luck.  Ftell apparently just blindly reads the
  25. file position indicator also and returns the result.
  26.  
  27. My final attempt was to simply go ahead and try to write to the file using
  28. fwrite, assuming that if a previous fseek were out of range the fwite would
  29. return failure.  Suprisingly, this didn't work either.  fwrite returned a
  30. value indicating it worked.  I don't know what fwrite actually did, if
  31. anything.  Any suggestions are appreciated.
  32.  
  33. Thanks,  Ray Mitchell
  34.