ios.gets doesn't seem to work as advertised 
Author Message
 ios.gets doesn't seem to work as advertised

I've got the following script in a Win2000SP3 environment running Ruby
1.6.8:

[snip]
        while rec = File.new(fullFileName).gets("\r\n")      
                puts "Record: "+rec
        end

for a file whose content is say:

AAA
BBB
CCC

with \r\n as the record separator (determined by viewing with a hex
editor).  What I expected is:

Record: AAA
Record: BBB
Record: CCC

What I got is:

Record: AAA
BBB
CCC
Record: AAA
BBB
CCC
[ad nauseum]

So it appears that IO$gets returns a array of "lines" again and again,
rather than each "line" successively, as described in Programming
Ruby.

The kernel$gets works by returning individual lines and recognizes the
\r\n delimiter automatically. I had to go to the IO version because I
wanted to read from multiple files and needed to know the name of the
file being read, which didn't seem possible if I passed an array to
ARGV.  Resetting ARGV with different filenames was flagged as an
error.

I don't want to read the entire file into memory for several reasons.
My application may encounter excessively large files.  Secondly, my
application runs in a thread that processes the latest records added
to the file and idles when there are no new records to be inspected.

Any suggestions?

TIA,
Richard



Mon, 21 Nov 2005 11:58:24 GMT  
 ios.gets doesn't seem to work as advertised

Quote:

> I've got the following script in a Win2000SP3 environment running Ruby
> 1.6.8:

> [snip]

> while rec = File.new(fullFileName).gets("\r\n")
> puts "Record: "+rec
> end

> [snip]

> Any suggestions?

> TIA,
> Richard

You're re-opening the file each time and reading
the the whole (haven't examined why) file.

This should do it ...

File.open('test.dat') do |fi|
  while rec = fi.gets
    puts "Record: " << rec
  end
end

# Record: AAA
# Record: BBB
# Record: CCC

daz



Mon, 21 Nov 2005 13:35:41 GMT  
 ios.gets doesn't seem to work as advertised
Hi All,

I solved one part of my problem, i.e. how to loop through individual
lines in a given text file, by using IO.foreach, e.g.:

IO.foreach(fullFileName) { |rec| processRec(rec) }

That routine recognized the \r\n delimiter implicitly.  However, it
surprised me by returning an empty record after the final char(s),
i.e. the delimiter.  That wasn't mentioned in The Pragmatic
Programmer's Guide.  Am I misinterpreting anything here?

For the second part, how to continually monitor a file to get the next
unread record or an "EOF" return code allowing the thread to wait and
again test for the availability of an unread record (appended by some
other thread), I think I need to access a Win32 routine.  I'm going to
check that out now.  If anyone has a suggestion about this approach,
I'd appreciate hearing about it.

Regards,
Richard



Mon, 21 Nov 2005 21:47:09 GMT  
 ios.gets doesn't seem to work as advertised
Hi Daz,

Thanks not only to the succint solution to my problem:

Quote:
> File.open('test.dat') do |fi|
>   while rec = fi.gets
>     puts "Record: " << rec
>   end
> end

but for your explanation of the flaw in my approach.  

Regards,
Richard



Mon, 21 Nov 2005 22:17:59 GMT  
 ios.gets doesn't seem to work as advertised
I made a mistake in my post:

Quote:
> That routine recognized the \r\n delimiter implicitly.  However, it
> surprised me by returning an empty record after the final char(s),
> i.e. the delimiter.  That wasn't mentioned in The Pragmatic
> Programmer's Guide.  Am I misinterpreting anything here?

I forgot to look at the hex version of the file before posting the
above lament.  It turns out the file had an extra \r\n pair at the
end, so it did include an empty record.  Sorry for the false alarm.

Richard



Tue, 22 Nov 2005 02:54:22 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. expect's full_buffer doesn't seem to work

2. set VIDEO_MODE_RESTRICTION=CGA doesn't seem to work

3. setting LD_LIBRARY_PATH doesn't seem to work?

4. proc doesn't seem to work in NASM

5. Micro Focus CALL-CONVENTION 8 doesn't seem to work

6. Adagide install doesn't seem to work?

7. Temporarily changing error_reporting()...doesn't seem to work

8. problem with exec: import doesn't seem to work

9. ldap.modify_s doesn't seem to work

10. update doesn't seem to work with scale

11. BLT installs but doesn't work, and TkTable doesn't build

12. Gets seems not to work in expect

 

 
Powered by phpBB® Forum Software