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.arachnoid.com