read in file

C

Chris Daves

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
 
J

Junyoung Kim

Chris said:
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
 
C

Chris Daves

Junyoung said:
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
 
J

Junyoung Kim

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"
 
L

Lee Jarvis

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
 
S

Sebastian Hungerecker

Chris said:
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
 
S

Sebastian Hungerecker

Mike said:
File.open('out.txt', 'w') do |out|
=A0 File.open('test.txt', 'r').each do |line|
=A0 =A0 out.puts line if (line.size > 4)
=A0 end
end

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

HTH,
Sebastian
=2D-=20
NP: Katatonia - Day
Jabber: (e-mail address removed)
ICQ: 205544826
 
P

Phrogz

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?
 
P

Phrogz

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.
 
M

Mike McKinney

[Note: parts of this message were removed to make it a legal post.]

very good point!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,059
Members
48,769
Latest member
Clifft

Latest Threads

Top