Best way to read data?

Bill,

Thanks for the reply!

while(line = gets)
p line
end

I’m getting:
C:\tmp>2.rb < "data.dat"
C:\tmp\2.rb:1:in `gets’: Bad file descriptor (Errno::EBADF)
from C:\tmp\2.rb:1

Using Win2K. :frowning:

···

############
Subject: Re: Best way to read data? From: “Bill Kelly” billk@cts.com Date:
Fri, 6 Dec 2002 02:03:38 +0900 References: 57777

Hi, From: christopher.j.meisenzahl@citicorp.com > > What’s the best Ruby
idiom for reading data from a data file one line at a time > so that I could
run the program with something like: > my_app.rb < data.txt A way to read lines
from STDIN is: while(line = gets) # do something with line end Or, to read
lines from a particular file by name: IO.foreach(“data.txt”) do |line| # do
something with line end Hope this helps, Bill

Christopher J. Meisenzahl CPS, CSTE
Senior Software Testing Consultant
Spherion
christopher.j.meisenzahl@citicorp.com

Hi Christopher,

Bill,

Thanks for the reply!

while(line = gets)
p line
end

I’m getting:
C:\tmp>2.rb < “data.dat”
C:\tmp\2.rb:1:in `gets’: Bad file descriptor (Errno::EBADF)
from C:\tmp\2.rb:1

Using Win2K. :frowning:

Hmm…

ruby test2.rb < “test2.rb”

got:
got:
got: while(line = gets)
got: print “got: #{line}”
got: end
got:

ruby -v

ruby 1.6.6 (2001-12-26) [i586-mswin32]

ver

Microsoft Windows 2000 [Version 5.00.2195]

Sorry, I don’t understand why your code isn’t working. Is “data.dat”
a text file or a binary file? (Just grasping at straws…)

Bill

···

From: christopher.j.meisenzahl@citicorp.com

Hi,

while(line = gets)
p line
end

I’m getting:
C:\tmp>2.rb < “data.dat”
C:\tmp\2.rb:1:in `gets’: Bad file descriptor (Errno::EBADF)
from C:\tmp\2.rb:1

Using Win2K. :frowning:

It’s a problem of cmd.exe which doesn’t pass the standard input
handle when opening an associated file.

c:\home\nobu> type foo.c
#include <windows.h>
#include <stdio.h>

int main()
{
printf(“stdin=%x\n”, GetStdHandle(STD_INPUT_HANDLE));
return 0;
}
c:\home\nobu> $ gcc -mno-cygwin foo.c -o foo.exe
c:\home\nobu> ftype foo=c:\home\nobu\foo.exe
foo=c:\home\nobu\foo.exe
c:\home\nobu> assoc .foo=foo
.foo=foo
c:\home\nobu> echo. > 1.foo
c:\home\nobu> 1.foo
stdin=0
c:\home\nobu> foo
stdin=150

···

At Fri, 6 Dec 2002 02:28:08 +0900, christopher.j.meisenzahl@citicorp.com wrote:


Nobu Nakada

Interesting.

A little bit of testing shows that

ruby foo.rb < foo.rb

works, while

foo.rb < foo.rb

gives the error Chirstopher is seeing. Why would this
be?

Also, I’m surprised noone recommended ‘readlines’
foo.rb
puts readlines

works fine for me… nice and clean

Jason

···

— Bill Kelly billk@cts.com wrote:

Hi Christopher,

From: christopher.j.meisenzahl@citicorp.com

Bill,

Thanks for the reply!

while(line = gets)
p line
end

I’m getting:
C:\tmp>2.rb < “data.dat”
C:\tmp\2.rb:1:in `gets’: Bad file descriptor
(Errno::EBADF)
from C:\tmp\2.rb:1

Using Win2K. :frowning:

Hmm…

ruby test2.rb < “test2.rb”

got:
got:
got: while(line = gets)
got: print “got: #{line}”
got: end
got:

ruby -v

ruby 1.6.6 (2001-12-26) [i586-mswin32]

ver

Microsoft Windows 2000 [Version 5.00.2195]

Sorry, I don’t understand why your code isn’t
working. Is “data.dat”
a text file or a binary file? (Just grasping at
straws…)

Bill

I wrote:

while(line = gets)
p line
end

I’m getting:
C:\tmp>2.rb < “data.dat”
C:\tmp\2.rb:1:in `gets’: Bad file descriptor (Errno::EBADF)
from C:\tmp\2.rb:1

Using Win2K. :frowning:

ruby test2.rb < “test2.rb”

Sorry, I don’t understand why your code isn’t working. Is “data.dat”
a text file or a binary file? (Just grasping at straws…)

Ahh! Oops, yes, if I invoke test2.rb directly, instead of running ruby
explicitly, I get the same error:

test2.rb < “test2.rb”

P:\code\ruby\bkelly\test2.rb:3:in `gets’: Bad file descriptor (Errno::EBADF)
from P:\code\ruby\bkelly\test2.rb:3

Don’t know why… :frowning:

Bill

Maybe you should try without the doule-quotes, unless the filename has
them… Like so:

2.rb < data.dat

– Nikodemus

···

On Fri, 6 Dec 2002, Bill Kelly wrote:

C:\tmp>2.rb < “data.dat”
C:\tmp\2.rb:1:in `gets’: Bad file descriptor (Errno::EBADF)
from C:\tmp\2.rb:1

Hi –

Interesting.

A little bit of testing shows that

ruby foo.rb < foo.rb

works, while

foo.rb < foo.rb

gives the error Chirstopher is seeing. Why would this
be?

Maybe if it’s running as an executable without a #! line
(so the shell/OS doesn’t know it’s a Ruby program).

Also, I’m surprised noone recommended ‘readlines’
foo.rb
puts readlines

works fine for me… nice and clean

It’s different though; it doesn’t output anything until EOF
(as opposed to one line at a time).

print while gets

might do it (if one doesn’t mind the Perlian magic :slight_smile:

David

···

On Fri, 6 Dec 2002, Jason Persampieri wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav