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
--
Posted via http://www.ruby-forum.com/.
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
···
--
Posted via http://www.ruby-forum.com/\.
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
···
--
NP: In Flames - Everdying
Jabber: sepp2k@jabber.org
ICQ: 205544826
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
···
--
Posted via http://www.ruby-forum.com/\.
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 Hungerecker <sepp2k@googlemail.com> wrote:
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
--
NP: In Flames - Everdying
Jabber: sepp2k@jabber.org
ICQ: 205544826
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"
···
--
Posted via http://www.ruby-forum.com/.
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
···
--
NP: Katatonia - Day
Jabber: sepp2k@jabber.org
ICQ: 205544826
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
···
--
Posted via http://www.ruby-forum.com/.
He is using the block form of File.open, which is guaranteed to close
the file at the end. Right?
···
On Dec 13, 8:27 am, Sebastian Hungerecker <sep...@googlemail.com> 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.
Nevermind; my reading comprehension seems to be turned off this
morning.
···
On Dec 13, 10:29 am, Phrogz <phr...@mac.com> wrote:
On Dec 13, 8:27 am, Sebastian Hungerecker <sep...@googlemail.com> > 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?