[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

read in file

Chris Daves

12/13/2007 1:59:00 AM

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

10 Answers

Junyoung Kim

12/13/2007 2:16:00 AM

0

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

Chris Daves

12/13/2007 2:17:00 AM

0

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

Junyoung Kim

12/13/2007 8:04:00 AM

0

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

Lee Jarvis

12/13/2007 8:14:00 AM

0

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

Sebastian Hungerecker

12/13/2007 2:54:00 PM

0

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

mike.s.mckinney

12/13/2007 3:20:00 PM

0

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

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

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

Sebastian Hungerecker

12/13/2007 3:27:00 PM

0

Mike McKinney wrote:
> 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: sepp2k@jabber.org
ICQ: 205544826

Phrogz

12/13/2007 5:29:00 PM

0

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?

Phrogz

12/13/2007 5:30:00 PM

0

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?

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

mike.s.mckinney

12/13/2007 6:17:00 PM

0

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

very good point!

On Dec 13, 2007 10:27 AM, Sebastian Hungerecker <sepp2k@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.
>
> HTH,
> Sebastian
> --
> NP: Katatonia - Day
> Jabber: sepp2k@jabber.org
> ICQ: 205544826
>
>