how to write file before EOF

S

Simon Strandgaard

how to write a file before its EOF
by giving some position .


If you just want to append to a file, then you can do like this:

irb(main):001:0> File.open("test", "a") {|f| f.write('a') }; IO.read("test")
=> "a"
irb(main):002:0> File.open("test", "a") {|f| f.write('b') }; IO.read("test")
=> "ab"
irb(main):003:0> File.open("test", "a") {|f| f.write('c') }; IO.read("test")
=> "abc"
irb(main):004:0>


if you want to seek backwards and write stuff

irb(main):005:0> File.open("test", "w") {|f| f.write('abcdefg') };
IO.read('test')
=> "abcdefg"
irb(main):006:0> File.open("test", "r+") {|f| f.seek(-5,
IO::SEEK_END); f.write("XYZ") }; IO.read('test')
=> "abXYZfg"
irb(main):007:0>
 
G

Gabriele Marrone

how to write a file before its EOF
by giving some position .

If you mean appending data to a file (not inserting data in the
middle of a file), you can just open it with the "a" mode:

File.open("mylog.txt", "a") do |f|
f.puts "adding this line at the end"
end

However, if you want to insert data in the middle of a file without
overwriting existing data, that's not so easy, as Paul pointed out.
 
S

Siva Prakasam

Paul said:
You do understand that if you write to a file at an arbitrary position,
it
will overwrite was was there before, yes? A insertion, if that is what
you
actually have in mind, is a bit more complicated (but that can also be
done).

Look at IO:seek.

thanks for suggestion

yes, i want to insert a file without overwriting its existing content.
i looked for IO:SEEK_CUR and IO:SEEK_SET which will set the position.but
it overwrites the existing content. how to do that?
 
T

Timothy Hunter

Siva said:
yes, i want to insert a file without overwriting its existing content.
i looked for IO:SEEK_CUR and IO:SEEK_SET which will set the position.but
it overwrites the existing content. how to do that?
You'll have to make a new file. Copy the old file contents up to the
point you want to insert the new material, insert the new material, then
copy the remainder of the old file. After the new file is safely closed,
delete the old file.
 
M

M. Edward (Ed) Borasky

Timothy said:
You'll have to make a new file. Copy the old file contents up to the
point you want to insert the new material, insert the new material,
then copy the remainder of the old file. After the new file is safely
closed, delete the old file.
It depends on how big the file is and how lazy a programmer you are. For
small files and lazy programmers, read the whole file into memory, edit
it in memory and write it back out. :)
 
U

Une bévue

Paul Lutus said:
Answer number one: why don't you find out by doing an experiment? Surely you
don't expect to be able to develop software for a particular operating
system without actually doing tests on that OS, do you?

Nope, others will test on win*, the question was to prevent some prob,
because, with some languages (php) such thing as the little 'a' might
change from *nix to win*.
Answer number two: yes, it works on Windows. It is a tautology of file
processing, and if it did not work, there wouldn't be anything else
discussed on this newsgroup.

My experiment, particularly on win*, and only with Java*, make me
thinking :

file processing != tautology

;-)

* I do not have any experiment on ruby/X-plaform for the time being.
 

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top