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