Problem with seeking in existing files.

G

Guest

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. I am using sysread
and syswrite to work with the file.

The problem is that seek doesn't seem to work. At
first I thought it might be regarding the end of the
original file as the 0 point, but some more testing
showed that even with an empty file, seek was not
changing the position of the file pointer.

I did some tests in IRB with a file opened with the
same mode, and seek seemed properly in it. Changing
the mode in the program to 'ab' did not work. (And I'm
eventually going to need read-write capability
anyway...)

Anyone know what could be causing this problem?

-Morgan.

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

Yukihiro Matsumoto

Hi,

In message "Problem with seeking in existing files."

|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. I am using sysread
|and syswrite to work with the file.

I don't know what platform you are working on. But in general, stdio
does not work well with "a" (append) mode and seeking. It's stdio
restriction.

matz.
 
M

Martin Weber

Win32.

What IO commands should be used with append mode?

Dunno about win32 in general, but why not simply use File.open/new ?


f = File.new("ayaken", File::CREAT|File::RDWR, 0644)
f.puts("banzai!");
f.close
f = File.new("ayaken", File::RDWR)
f.seek(8);
f.puts("ayaken!");

=> ayaken now contains (at least on Unix)
banzai!\n
ayaken!\n

(on windows probably have to seek one further for the \r)

-Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,139
Messages
2,570,805
Members
47,351
Latest member
LolaD32479

Latest Threads

Top