# Problem reading small files - stringio problem?

**URL:** <https://www.ruby-forum.com/t/problem-reading-small-files-stringio-problem/138752>\
**Category:** Ruby\
**Created:** [May 27, 2008, 1:38pm UTC](https://www.ruby-forum.com/t/problem-reading-small-files-stringio-problem/138752 "2008-05-27T13:38:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Singeo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ec8ea/32.png) [@Singeo](https://www.ruby-forum.com/u/Singeo)\
**Post date:** [May 27, 2008, 1:38pm UTC](https://www.ruby-forum.com/t/problem-reading-small-files-stringio-problem/138752/1 "2008-05-27T13:38:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Singeo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ec8ea/32.png) [@Singeo](https://www.ruby-forum.com/u/Singeo)\
**Post date:** [May 27, 2008, 2:51pm UTC](https://www.ruby-forum.com/t/problem-reading-small-files-stringio-problem/138752/2 "2008-05-27T14:51:39Z")

</div>

I think your problem is: open(‘sample.txt’, ‘w’). You either open a file  
for  
append or write, but not both. Remove this line and it might work.

Shorter also might be:

File.open(‘sample.txt’, ‘a’) do |out|  
(‘A’…‘Z’).each do |c|  
out.puts File.open("#{c}.html", ‘r’).read  
end  
end

martin

---

<div class="post-metadata">

**Author:** ![Singeo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ec8ea/32.png) [@Singeo](https://www.ruby-forum.com/u/Singeo)\
**Post date:** [May 27, 2008, 2:54pm UTC](https://www.ruby-forum.com/t/problem-reading-small-files-stringio-problem/138752/3 "2008-05-27T14:54:35Z")

</div>

2008/5/27 Singeo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> dummy = letters[index] + “.html”  
> 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

---

<div class="post-metadata">

**Author:** ![Singeo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ec8ea/32.png) [@Singeo](https://www.ruby-forum.com/u/Singeo)\
**Post date:** [May 27, 2008, 6:28pm UTC](https://www.ruby-forum.com/t/problem-reading-small-files-stringio-problem/138752/4 "2008-05-27T18:28:10Z")

</div>

Martin/Robert, thank you both for your quick replies. Both your  
suggestions work, thanks for the valuable advice.

I’m still puzzled as to why my code (however bad…) was working with  
all but the smaller files (5 of the 26 all less than 20k)). But I can  
now move forward.

Regards

Singeo

On May 27, 8:52 pm, “Robert K.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
