U
Uday Thokala
Hi,
I am newbie to Ruby so please spare me if the question looks silly. My
question is how to append data at the beginning of a file?
Suppose I have a file named test.rb which contains some text, say
"This is first line
This is second line"
Now if I wanted to append some data at the beginning of the file, lets
say
"This line has to be appended at the beginning of the file"
My program
filename = File.open("test","a") do |f|
f.puts "This line should appear at the top of each file";
f.close();
end
is appending at the end of the file. So the output is:
"This is first line
This is second line
This line has to be appended at the beginning of the file"
I searched the forums and found that if I use IO:seek and then try to
append data to the existing file the earlier content which are in the
first lines will get replaced. Is there any easy solution so that I can
get a final output like:
"This line has to be appended at the beginning of the file
This is first line
This is second line"
I am newbie to Ruby so please spare me if the question looks silly. My
question is how to append data at the beginning of a file?
Suppose I have a file named test.rb which contains some text, say
"This is first line
This is second line"
Now if I wanted to append some data at the beginning of the file, lets
say
"This line has to be appended at the beginning of the file"
My program
filename = File.open("test","a") do |f|
f.puts "This line should appear at the top of each file";
f.close();
end
is appending at the end of the file. So the output is:
"This is first line
This is second line
This line has to be appended at the beginning of the file"
I searched the forums and found that if I use IO:seek and then try to
append data to the existing file the earlier content which are in the
first lines will get replaced. Is there any easy solution so that I can
get a final output like:
"This line has to be appended at the beginning of the file
This is first line
This is second line"