[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

size change after file writing

Li Chen

7/30/2007 6:50:00 PM

Hi all,

I write a small script to read old files and write to new files. Either
reading line by line or whole file at once then I ask Ruby to write to
new files. The new file is much smaller than the old one. I also find
that the new file losses many information. How to explain this?

Thanks,

Li

#################script-1 read file line by line and write to a new file
path='C:\\Allo12-Y2'
file_names=[]

Dir.open(path).each do|f|
file_names<<path+"\\#{f}" unless f=~/^\.\.*$/
end

file_names.each do|f|
new_file=File.open("#{f}b",'w')
File.open(f,'r') do|file|
file.each_line{|line|new_file.print line }
end
end


###################sctript-2 read whole file and write to a new file


path='C:\\Allo12-Y2'
file_names=[]

Dir.open(path).each do|f|
file_names<<path+"\\#{f}" unless f=~/^\.\.*$/
end


file_names.each do|f|
file=File.open(f,'r') #open file for read
new_file=File.open("#{f}b",'w')#open file for write
new_file.print file.read #write old file to new file
end

### size in old file: AlloY2.001 is 609kb
size in new file: AlloY2.001b is 47bk
--
Posted via http://www.ruby-....

4 Answers

Nobuyoshi Nakada

7/30/2007 7:43:00 PM

0

Hi,

At Tue, 31 Jul 2007 03:50:24 +0900,
Li Chen wrote in [ruby-talk:262503]:
> I write a small script to read old files and write to new files. Either
> reading line by line or whole file at once then I ask Ruby to write to
> new files. The new file is much smaller than the old one. I also find
> that the new file losses many information. How to explain this?

Use "rb" and "wb" mode for binary files, or better, use FileUtils.cp.

--
Nobu Nakada

Li Chen

7/30/2007 8:00:00 PM

0

Nobuyoshi Nakada wrote:

> Use "rb" and "wb" mode for binary files, or better, use FileUtils.cp.

Thank you very much and they work well.

Li

--
Posted via http://www.ruby-....

Jim G.

9/7/2012 7:41:00 PM

0

Mason Barge sent the following on Thu, 06 Sep 2012 14:20:08 -0400:
>
> Oh, I forgot, this was the first airliner in history to perform the
> aerodynamically impossible trick of falling straight down.

Before crashing into a field. Where someone is peeing.

--
Jim G. | Waukesha, WI
"You will create an evil of your own making." -- Knights Templar Grandmaster Data

shawn

9/7/2012 8:15:00 PM

0

On Fri, 07 Sep 2012 14:40:36 -0500, Jim G.
<jimgysin@geemail.com.invalid> wrote:

>Mason Barge sent the following on Thu, 06 Sep 2012 14:20:08 -0400:
>>
>> Oh, I forgot, this was the first airliner in history to perform the
>> aerodynamically impossible trick of falling straight down.
>
>Before crashing into a field. Where someone is peeing.

Someone with long dark hair.