PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Problem reading small files - stringio problem?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Problem reading small files - stringio problem?

Réponse
 
LinkBack Outils de la discussion
Vieux 27/05/2008, 12h37   #1
Singeo
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Problem reading small files - stringio problem?

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

  Réponse avec citation
Vieux 27/05/2008, 13h48   #2
Martin Boese
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problem reading small files - stringio problem?

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


On Tuesday 27 May 2008 12:37:37 Singeo wrote:
> 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




  Réponse avec citation
Vieux 27/05/2008, 13h52   #3
Robert Klemme
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problem reading small files - stringio problem?

2008/5/27 Singeo <singeo.sg@gmail.com>:
> 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

  Réponse avec citation
Vieux 27/05/2008, 17h26   #4
Singeo
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problem reading small files - stringio problem?

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=A0pm, "Robert Klemme" <shortcut...@googlemail.com>
wrote:
> 2008/5/27 Singeo <singeo...@gmail.com>:
>
>
>
> > 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=3D("A".."Z").to_a
> > link_array =3D []

>
> > for index in (0...letters.length)
> > =A0dummy =3D letters[index] + ".html"
> > =A0link_array << dummy
> > end

>
> > open('sample.txt','w')
> > for index in (0...link_array.length)
> > =A0guide=3DFile.open(link_array[index], "r+")
> > =A0html =3D guide.read
> > =A0open('sample.txt','a') do |f|
> > =A0 =A0f.puts guide
> > =A0end
> > end

>
> This has nothing to do with StringIO. =A0You do not close all your file
> descriptors properly. =A0You should make it a habit to use the block
> form of File.open.
>
> Note also that you can use ("A" .. "Z") directly for iterating. =A0Also,
> 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|
> =A0 for ch in "A".."Z"
> =A0 =A0 out.write(File.read(ch + ".html"))
> =A0 end
> end
>
> If your files are larger, you should copy chunkwise
>
> File.open("sample.txt", "a") do |out|
> =A0 for ch in "A".."Z"
> =A0 =A0 File.read(ch + ".html") do |in|
> =A0 =A0 =A0 while buffer =3D in.read(1024)
> =A0 =A0 =A0 =A0 out.write buffer
> =A0 =A0 =A0 end
> =A0 =A0 end
> =A0 end
> end
>
> Kind regards
>
> robert
>
> --
> use.inject do |as, often| as.you_can - without end


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 10h54.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12126 seconds with 12 queries