Martin Weber
10/30/2003 6:23:00 PM
On Fri, Oct 31, 2003 at 02:57:13AM +0900, agemoagemo@yahoo.com wrote:
>
> --- Warren Brown <wkb@airmail.net> wrote:
> > Morgan,
> >
> > > I'm trying to write a program that will be writing
> > > data to an existing file, that will not
> > necessarily
> > > be done in the physical file order. I used mode
> > > 'ab+' to open the file, as only the 'a' modes
> > allow
> > > writing without changing the existing file.
> >
> > It sounds like what you really want is to open
> > the file with a mode
> > of 'r+b'. This allows reading and writing of the
> > file without
> > truncating the file (like 'w+b'). As you mentioned,
> > a mode of 'ab'
> > opens the file for writing only (at end of file).
> > To open for reading
> > and writing (at end of file) you would need a mode
> > of 'a+b'. I believe
> > a mode of 'ab+' is technically incorrect on most
> > platforms, although it
> > may work on some.
>
> Well, I don't know about technical incorrectness, but
> mode 'a+b' is identified as an illegal access mode,
> wheras 'ab+' will execute.
>
> Using mode 'rb+' seems to produce the behavior I
> need... when the file exists. When it doesn't,
> however, it produces:
> ...
File.new/open(filename, File::CREAT|File::RDWR) ...
File::CREAT means, create the file if it doesn't exist.
"a" is equivalent to File::CREAT|File::WRONLY +
seek(0, IO::SEEK_END)
w is File::CREAT|File::WRONLY|File::TRUNC
r is File::RDONLY
r+ is File::RDWR
See, File::CREAT is lacking here. With it there would be
no error.
w+ is File::TRUNC|File::CREAT|File::RDWR
b means binary on windows, but on unix that's a noop, as
there is no artificial distinction between file types. Content
is what counts, so I can't tell you what's the File:: equivalent
to it. Just don't use the strings, use the File:: constants.
-Martin