[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

how to write file before EOF

Siva Prakasam

11/28/2006 4:36:00 AM


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

--
Posted via http://www.ruby-....

11 Answers

Paul Lutus

11/28/2006 7:29:00 AM

0

Siva Prakasam 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.

--
Paul Lutus
http://www.ara...

Simon Strandgaard

11/28/2006 7:38:00 AM

0

On 11/28/06, Siva Prakasam <siva@360in.com> 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>


--
Simon Strandgaard
http://opc...

Gabriele Marrone

11/28/2006 7:42:00 AM

0


On 28/nov/06, at 05:36, Siva Prakasam 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 Marrone



Siva Prakasam

11/28/2006 9:10:00 AM

0

Paul Lutus wrote:
> Siva Prakasam 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?

--
Posted via http://www.ruby-....

pere.noel

11/28/2006 9:36:00 AM

0

Gabriele Marrone <gabriele.marrone@gmail.com> wrote:

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

This one works also on win* ?
--
une bévue

Tim Hunter

11/28/2006 12:39:00 PM

0

Siva Prakasam 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.

M. Edward (Ed) Borasky

11/28/2006 2:31:00 PM

0

Timothy Hunter wrote:
> Siva Prakasam 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.
>
>
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.blo...

If God had meant for carrots to be eaten cooked, He would have given rabbits fire.


Paul Lutus

11/28/2006 4:26:00 PM

0

Une bévue wrote:

> Gabriele Marrone <gabriele.marrone@gmail.com> wrote:
>
>> File.open("mylog.txt", "a") do |f|
>> f.puts "adding this line at the end"
>> end
>
> This 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.

--
Paul Lutus
http://www.ara...

Paul Lutus

11/28/2006 4:40:00 PM

0

Siva Prakasam wrote:

> Paul Lutus wrote:
>> Siva Prakasam 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?

There are several ways to do this. The simplest approach:

1. Read the entire file.

2. Insert using a regular expression or an index, depending on the
requirement.

3. 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.

--
Paul Lutus
http://www.ara...

pere.noel

11/28/2006 5:15:00 PM

0

Paul Lutus <nospam@nosite.zzz> 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.
--
une bévue