Random Access files

John Gordon gordon at osiris.cso.uiuc.edu
Tue Nov 20 12:45:54 AEST 1990


Tony R. Marasco <TRM900 at psuvm.psu.edu> writes:

>Hello.  I am trying to use random access files and have run into some
>problems...

>I am using _fopen_ with a "w+b" parameter to create the file (if it doesn't
>exist).  If it exists, I use "r+b".  Otherwise it seems to erase the file
>like PASCAL's _rewrite_.  How am I doing so far??

	Opening a file in "w" mode is supposed to destroy existing files.  If
you don't want to do this, open in "a" mode.

>I am also using _fseek_ to position the file pointer (recnum*size of struct)
>with SEEK_SET.  I do this before each _fread_ & _fwrite_.  The program
>seems to "lose" a few bytes at the beginning of each _fread_.

	The first record number should be at recnum 0.  Are you starting at
recnum 1?


>What is more astounding is I can write a record that doesn't exist, but
>I can't update a record that already exists.

	I think that opening in "a" might work.



More information about the Comp.lang.c mailing list