Re: Problem reading data from stdin using EOF(0) in version 6.0 and 7.0 [message #64395] |
Mon, 22 December 2008 07:52  |
Allan Whiteford
Messages: 117 Registered: June 2006
|
Senior Member |
|
|
kiwing.to@gmail.com wrote:
> I am not sure if the EOF() function in IDL version newer than 5.2
> really has some problem or some setting has to be adjusted.
>
> I just want to read data from the stdin and automatically stop at
> the end-of-file. The following codes work for version 5.2 but not
> for version 6.0 nor version 7.0.
>
> Just want to know if anyway around it ?
>
> #######################
> ;--- test_eof.pro -----------------
> pro test_eof
> a=float(1)
> while not eof(0) do begin
> read, a
> print, a
> endwhile
> end
> ;---- end of 'test_eof.pro' -----
>
> #######################
> ;---- test_eof.idl ----------------
> .compile test_eof
> .run test_eof
> exit
> ;---- end of 'test_eof.idl' ------
> #######################
>
> When I ran it using "idl test_eof.idl"
> I got error at the end of my input.
>
> ############################################################ #
> IDL Version 7.0 (linux x86 m32). (c) 2007, ITT Visual Information
> Solutions
> Installation number: 60188.
> Licensed for use by: Academia Sinica
>
> % Compiled module: $MAIN$.
> % Compiled module: $MAIN$.
> : 1
> 1.00000
> : 2
> 2.00000
> : ^D
> % READ: Error encountered reading from file. Unit: 0
> File: <stdin>
> % Execution halted at: $MAIN$ 3 /home/ericto/test_eof.pro
> ############################################################ ##
You can't check for eof(0) when you're giving input from the keyboard.
It can't know what you're going to type next so it returns a 0 and then
control passes to your "read, a" statement which subsequently fails when
you pass it an EOF character. The eof function never gets to see that
you typed Ctrl-D.
If you read from a file like this:
idl test_eof.idl < file.dat
then all will be well.
I don't know why it worked on IDL5.2 - I can only find versions 5.1 and
5.3 (typical!) and in both of them I see the same behaviour.
Maybe in IDL5.2 the EOF function detected it was attached to a keyboard
and blocked until it got another byte from the keyboard? It seems
unlikely but I think that's the only way the above example would have
worked in 5.2.
Thanks,
Allan
|
|
|