From: Robert Klemme Date: 2008-05-27T21:52:54+09:00 Subject: Re: Problem reading small files - stringio problem? 2008/5/27 Singeo : > Hi, I wonder if someone can take a quick look at this code and tell me > where I'm going wrong. The small files (less than 20k?) that I read in > this array are not appearing in the output file. I understand this is > a problem to do with stringio but have not found a way to fix it. > Thanks. > > letters=("A".."Z").to_a > link_array = [] > > for index in (0...letters.length) > dummy = letters[index] + ".html" > link_array << dummy > end > > open('sample.txt','w') > for index in (0...link_array.length) > guide=File.open(link_array[index], "r+") > html = guide.read > open('sample.txt','a') do |f| > f.puts guide > end > end This has nothing to do with StringIO. You do not close all your file descriptors properly. You should make it a habit to use the block form of File.open. Note also that you can use ("A" .. "Z") directly for iterating. Also, it does not make sense to open and close "sample.txt" all the time. Also, why do you write the IO object to the file and not the content? It seems your code can be achieved by doing this: File.open("sample.txt", "a") do |out| for ch in "A".."Z" out.write(File.read(ch + ".html")) end end If your files are larger, you should copy chunkwise File.open("sample.txt", "a") do |out| for ch in "A".."Z" File.read(ch + ".html") do |in| while buffer = in.read(1024) out.write buffer end end end end Kind regards robert -- use.inject do |as, often| as.you_can - without end