Read in file

Hi,
I’ve created a simple program which will read in a file.

class Reference
references = ‘C:\Documents and Settings\Chris
Davies\Desktop\References.rb’

f = File.open(references, ‘r’)
file_data = f.read
f.close

puts file_data
end

but i want to take this file data and interpret each line of data and
the put each line into a new file line by line.

cheers

Chris Daves wrote:

Hi,
I’ve created a simple program which will read in a file.

class Reference
references = ‘C:\Documents and Settings\Chris
Davies\Desktop\References.rb’

f = File.open(references, ‘r’)
file_data = f.read
f.close

puts file_data
end

but i want to take this file data and interpret each line of data and
the put each line into a new file line by line.

cheers

File.open(“path/your/file”) do |sFile|
while sLine = sFile.gets
puts YouCanSeeYourData
end
end

Junyoung Kim wrote:

Chris Daves wrote:

Hi,
I’ve created a simple program which will read in a file.

class Reference
references = ‘C:\Documents and Settings\Chris
Davies\Desktop\References.rb’

f = File.open(references, ‘r’)
file_data = f.read
f.close

puts file_data
end

but i want to take this file data and interpret each line of data and
the put each line into a new file line by line.

cheers

File.open(“path/your/file”) do |sFile|
while sLine = sFile.gets
puts YouCanSeeYourData
end
end

cheers for that, but how can i take that data and move it into a new
file line by line? i just want to move exactly the same data from an
existing file to a new file.

cheers

if you dont want to modify the existing file, you dont need to open
file. just duplicate it or copy it.

in my case, i will use fileutils(cp_r methods is only available on 1.8)

example)

require ‘fileutils’

include FileUtils

cp_r “existing”, “new”

Or if you do want to read and modify it…

new = File.open(‘filetowriteto’, ‘w’)
File.foreach(‘oldfile’) do |line|
new.puts line

end

or…

File.open(‘newfile’, ‘w’) do |file|
file.puts File.read(‘oldfile’)
end

or…

There are many ways, check out File# and FileUtils

Regards,
Lee

another way…

File.open(‘out.txt’, ‘w’) do |out|
File.open(‘test.txt’, ‘r’).each do |line|
out.puts line if (line.size > 4)
end
end

Here are some ways to pull each line from a file in other languages:
http://blog.huikau.com/2007/11/25/simple-file-io-in-different-dynamic-languages/

M

On Dec 13, 2007 9:54 AM, Sebastian H. [email protected]

Mike McKinney wrote:

File.open(‘out.txt’, ‘w’) do |out|
File.open(‘test.txt’, ‘r’).each do |line|
out.puts line if (line.size > 4)
end
end

That leaves test.txt open which is not good. Don’t use File.open in
chains
like that. Or better yet: Don’t use File.open without a block at all.

HTH,
Sebastian

Chris Daves wrote:

f = File.open(references, ‘r’)
file_data = f.read
f.close

That can be written as
file_data=File.read(“references”)
It’s generally better not to use File in a way that you have to manually
close
the file.

but i want to take this file data and interpret each line of data and
the put each line into a new file line by line.

File.open(outfile, “w”) do |of|
File.foreach(infile) do |line|
do_something_with line
of.puts line
end
end

HTH,
Sebastian

On Dec 13, 10:29 am, Phrogz [email protected] wrote:

That leaves test.txt open which is not good. Don’t use File.open in chains
like that. Or better yet: Don’t use File.open without a block at all.

He is using the block form of File.open, which is guaranteed to close
the file at the end. Right?

Nevermind; my reading comprehension seems to be turned off this
morning.

very good point!

On Dec 13, 2007 10:27 AM, Sebastian H. [email protected]

On Dec 13, 8:27 am, Sebastian H. [email protected]
wrote:

Mike McKinney wrote:

File.open(‘out.txt’, ‘w’) do |out|
File.open(‘test.txt’, ‘r’).each do |line|
out.puts line if (line.size > 4)
end
end

That leaves test.txt open which is not good. Don’t use File.open in chains
like that. Or better yet: Don’t use File.open without a block at all.

He is using the block form of File.open, which is guaranteed to close
the file at the end. Right?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs