From: Dimitri Aivaliotis Date: 2006-04-07T00:04:44+09:00 Subject: Re: Basic input/output question Hi Peter, On 4/6/06, Peter Bailey wrote: > Hello, > I spent my $40 on "Programming Ruby," but, it seems dedicated to C > programmer rocket scientists. I work in publishing and basically I just > need to open files, convert the data, and write the files, either to the > same file or to another. Can someone please help me to write a simple > structure to read a file "text1.txt", do regexes and stuff in it to > convert data, and then write the output to "text2.txt." A very general way to do this is: big = 10485760 # we'll deal with files larger than 10MB specially read = "text1.txt" # the file to be read write = "text2.txt" # the file to be written File.open( write, "w") do |w| File.open( read ) do |r| if File.size( read ) < big while line = r.gets # do somethin with line w << line end else# read the file in big-sized chunks while !f.eof? f.read( big ).split($/).each do |line| # do somethin with line w << line end end end end end - Dimitri