Hi,
I want to write some content to an existing file and I want to new
content be the first line of the file. How to do that?
Thanks
···
--
Posted via http://www.ruby-forum.com/.
Hi,
I want to write some content to an existing file and I want to new
content be the first line of the file. How to do that?
Thanks
--
Posted via http://www.ruby-forum.com/.
Hi,
I want to write some content to an existing file and I want to new
content be the first line of the file. How to do that?
Hi John,
You can't do it directly - there is no insert for disk files. What you have to do depends how big it is.
For file that will fit in RAM, load it, join with your first line, and save it all.
For larger files, you need to copy and rename.
Regards
Ian
On 19/05/2010 22:28, John Wu wrote:
Thanks
No virus found in this incoming message.
Checked by AVG - www.avg.com
Version: 9.0.819 / Virus Database: 271.1.1/2883 - Release Date: 05/19/10 07:26:00
> Hi,
>
> I want to write some content to an existing file and I want to new
> content be the first line of the file. How to do that?
[snip]
For file that will fit in RAM, load it, join with your first line, and
save it all.For larger files, you need to copy and rename.
Or for files whose contents you actually care about -- and many of those are
small files. Think about what happens if you crash.
The typical method is to create a temporary file, insert the data, append the
original file, then rename on top of the original file. Then the file is never
in a corrupted state, even temporarily.
On Wednesday, May 19, 2010 05:25:13 pm Ian Hobson wrote:
On 19/05/2010 22:28, John Wu wrote: