Hi folks,
New to Ruby, with little previous coding experience. Anyway, trying out
a few examples from a tut:
junkfile = File.new(“junk”, “r+”)
junkfile.write (‘junk line 1’)
junkfile.write (‘junk line 2’)
junkfile.rewind
puts junkfile.readline
puts junkfile.readline
This is the output:
No such file or directory - junk
C:/DATA/FILE/CODE/RUBY/prgs/junktest.rb:1:in `initialize’
Press ENTER to close the window…
What am I doing wrong?
Thanks.
r+ will open the file and give you read-write access iff the file is
exists. w+ and a+ create new files if it doesn’t exist, unlike r+. (w+
truncates, a+ appends)
Siddarth
Siddarth
On Sun, May 9, 2010 at 8:49 PM, Siddarth Chandrasekaran
Thanks. Question #2.
How can I write/read a specificied line from the file?
Or you could read it into an array, mess with it and write it back,
although that’s pretty ugly:
array = File.open(“junk”).collect
Siddarth
Hi David,
the only way I could think of right now would be to read all lines and
increment a counter while reading.
Am 10.05.2010 09:52, schrieb David Chapman:
Hi,
how about
File.readlines(“data.txt”)[n]?
Thorsten
Am 10.05.2010 10:21, schrieb Siddarth Chandrasekaran:
On Mon, May 10, 2010 at 10:28 AM, Thorsten H. [email protected] wrote:
Hi,
how about
File.readlines(“data.txt”)[n]?
File.open … do | f |
f.each_with_index do | line, line_nb |
…
end
end
HTH
Robert