[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Problem with seeking in existing files.

Thomas Morgan

10/30/2003 5:57:00 PM


--- 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:

in `initialize': No such file or directory - "new.zip"
(Errno::ENOENT)

Given the alternative, I'd be quite happy to work
around it by testing for the presence of a file, and
creating a new one if it doesn't exist... but I
haven't yet found a simple way to test for this.

-Morgan.

__________________________________
Do you Yahoo!?
Exclusive Video Premiere - Britney Spears
http://launch.yahoo.com/promos/brit...

1 Answer

Martin Weber

10/30/2003 6:23:00 PM

0

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