Files

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.

···

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

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

···

On Sun, May 9, 2010 at 7:48 PM, David Chapman <ideabolt@gmail.com> wrote:

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.
--
Posted via http://www.ruby-forum.com/\.

* iff the file exists.

Siddarth

···

On Sun, May 9, 2010 at 8:49 PM, Siddarth Chandrasekaran <chandrasekaran.siddarth@gmail.com> wrote:

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

On Sun, May 9, 2010 at 7:48 PM, David Chapman <ideabolt@gmail.com> wrote:

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.
--
Posted via http://www.ruby-forum.com/\.

Thanks. Question #2.

How can I write/read a specificied line from the file?

···

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

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:

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

<do something with array>

<write the array back into the file>

Siddarth

···

On Mon, May 10, 2010 at 4:19 AM, Bernd Ritter <commel@gmail.com> wrote:

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:

Thanks. Question #2.

How can I write/read a specificied line from the file?

Hi,

how about

File.readlines("data.txt")[n]?

Thorsten

···

Am 10.05.2010 10:21, schrieb Siddarth Chandrasekaran:

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

<do something with array>

<write the array back into the file>

Siddarth

On Mon, May 10, 2010 at 4:19 AM, Bernd Ritter<commel@gmail.com> wrote:
   

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:
     

Thanks. Question #2.

How can I write/read a specificied line from the file?

Thanks guys.

···

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

File.open ... do | f |
  f.each_with_index do | line, line_nb |
   ...
  end
end
HTH
Robert

···

On Mon, May 10, 2010 at 10:28 AM, Thorsten Hater <th@tp1.rub.de> wrote:

Hi,

how about

File.readlines("data.txt")[n]?

--
The best way to predict the future is to invent it.
-- Alan Kay