[vox-tech] [C] randomly accessing file data in C

Jeff Newmiller vox-tech@lists.lugod.org
Wed, 21 May 2003 07:48:19 -0700 (PDT)


On Tue, 20 May 2003, Mark K. Kim wrote:

> Never mind... got it figured out.  I had forgotten to put a couple flags.
> Heh... it happens now and then :P  I'm now using POSIX calls instead of
> the C library calls -- probably safer to interact directly with the kernel
> rather than through the C library -- one less layer of large file support
> to worry about.
> 
> Anyway, as always, Jeff has some productive things to say here so let
> me reply...:
> 
> On Tue, 20 May 2003, Jeff Newmiller wrote:
> 
> > You need to be careful how you open it... if you are using open, you
> > probably want O_RDWR mode... if fopen, "r+".
> 
> If I'm reading one file, then writing another, I only need "r" for the
> file I'm reading and "w" for the file I'm writing, don't I?  Why would
> you open it "r+"?

Read the manpage for fopen, and compare "r+" to "w+", as they behave
differently.  I thought you were reading and writing from the same file
when I wrote that... recall your opening text:

> > > I'm trying to access data in a file in C.  I want to read some
> > > portion of it, modify it, then rewrite that portion back into the
> > > file.


> Anyway, I'm now opening the output file with (O_WRONLY | O_CREAT |
> O_LARGEFILE).  Should I open it with (O_RDWR | O_CREAT | O_LARGEFILE)
> instead?

Only if you intended to read and write the same file.

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil@dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...2k
---------------------------------------------------------------------------