how to write a file before its EOF
by giving some position .
Siva P. wrote:
how to write a file before its EOF
by giving some position .
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.
On 11/28/06, Siva P. [email protected] wrote:
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>
On 28/nov/06, at 05:36, Siva P. wrote:
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.
Gabriele M. [email protected] wrote:
File.open(“mylog.txt”, “a”) do |f|
f.puts “adding this line at the end”
end
This one works also on win* ?
Siva P. wrote:
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.
Paul L. wrote:
Siva P. wrote:
how to write a file before its EOF
by giving some position .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?
Une bévue wrote:
Gabriele M. [email protected] wrote:
File.open(“mylog.txt”, “a”) do |f|
f.puts “adding this line at the end”
endThis one works also on win* ?
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?
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.
Siva P. wrote:
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?
There are several ways to do this. The simplest approach:
-
Read the entire file.
-
Insert using a regular expression or an index, depending on the
requirement. -
Write the new file content to the filesystem.
Example using a regular expression (skipping the file read and write):
a = “this is a test”
a.sub!(/(this is) (a test)/,"\1 not really \2")
now a = “this is not really a test”
Example using an index (again, skipping the read and write):
a = “this is a test”
a[8 … 8] = "not really "
now a = “this is not really a test”
Another approach, for large files, is to read from an input file while
simultaneously writing to an output file, character by character,
counting
characters as you go. When you get to the insertion point, insert the
desired text into the output stream, then resume copying characters from
the input file.
Timothy H. wrote:
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.
–
M. Edward (Ed) Borasky, FBG, AB, PTA, PGS, MS, MNLP, NST, ACMC(P)
http://borasky-research.blogspot.com/
If God had meant for carrots to be eaten cooked, He would have given
rabbits fire.
Une bévue wrote:
Paul L. [email protected] wrote:
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.
Not a chance. Never happen.
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
The point I am making is that certain canonical file processing tasks,
like
opening, reading, writing, appending to, and closing files must be
platform-independent. If they are not, the problem lies with the
implementation, not the conception (e.g. it’s a bug).
Paul L. [email protected] wrote:
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.