What's the difference? 
Author Message
 What's the difference?

Hi there,

When I try to read a file with only one blank line, using the
following code, I get different result. I can not figure out what's
the difference.

Any comment?

Thanks in advance!

Regards,
Jinsong

C234567
      PROGRAM TEST1
      INTEGER K
      READ(5,*) K
      WRITE(*,*) K
      END

D:\>a.exe < tmp.txt
At line 4 of file tmp.f (unit = 5, file = 'stdin')
fortran runtime error: End of file

C234567
      PROGRAM TEST
      INTEGER K
      READ(5,'(I2)') K
      WRITE(*,*) K
      END

D:\>a.exe < tmp.txt
           0



Sat, 10 Sep 2011 22:36:33 GMT  
 What's the difference?

Quote:
> Hi there,

> When I try to read a file with only one blank line, using the
> following code, I get different result. I can not figure out what's
> the difference.

> Any comment?

> Thanks in advance!

> Regards,
> Jinsong

> C234567
> ? ? ? PROGRAM TEST1
> ? ? ? INTEGER K
> ? ? ? READ(5,*) K
> ? ? ? WRITE(*,*) K
> ? ? ? END

> D:\>a.exe < tmp.txt
> At line 4 of file tmp.f (unit = 5, file = 'stdin')
> Fortran runtime error: End of file

> C234567
> ? ? ? PROGRAM TEST
> ? ? ? INTEGER K
> ? ? ? READ(5,'(I2)') K
> ? ? ? WRITE(*,*) K
> ? ? ? END

> D:\>a.exe < tmp.txt
> ? ? ? ? ? ?0

READ(5,*) tries to read a number and skips spaces and even empty
lines
to find such a number. READ(5,'(I2)') does not do that: it will simply
take the first two characters and attempts to convert that to a
number.

As there is nothing in the file, the first form reaches the end of the
file
whereas the second form just takes the empty line, converts it to
spaces
and (based on the format) turns them into zeros.

Regards,

Arjen



Sat, 10 Sep 2011 23:00:53 GMT  
 What's the difference?
Quote:

> Hi there,

> When I try to read a file with only one blank line, using the
> following code, I get different result. I can not figure out what's
> the difference.

> Any comment?

> Thanks in advance!

> Regards,
> Jinsong

> C234567
>       PROGRAM TEST1
>       INTEGER K
>       READ(5,*) K
>       WRITE(*,*) K
>       END

READ(5,*) K is a free format : a numerical value is expected in the file
tmp.txt; if you have one or more blank lines of course you have an end
of file

Quote:

> D:\>a.exe < tmp.txt
> At line 4 of file tmp.f (unit = 5, file = 'stdin')
> Fortran runtime error: End of file

> C234567
>       PROGRAM TEST
>       INTEGER K
>       READ(5,'(I2)') K
>       WRITE(*,*) K
>       END

> D:\>a.exe < tmp.txt
>            0

READ(5,'(I2)') K looks for an integer number located on the  first two
positions (I2) in tmp.txt; if there are blanks 0 is read

please find it in a Fortan course

B Bru



Sat, 10 Sep 2011 23:11:19 GMT  
 What's the difference?

Quote:

> When I try to read a file with only one blank line, using the
> following code, I get different result. I can not figure out what's
> the difference.

(snip)

Quote:
>      READ(5,*) K

(snip)

Quote:
>      READ(5,'(I2)') K

The former, list-directed input, reads cards, oops,
input records, until it finds one with non-blanks.

The latter reads one record, and takes the first two
columns of that record.  If they are both blank you
get zero.

This changed slightly from the Fortran 66 conversions,
which counted blank columns as zero.  In Fortran 66
with I2 conversion the input

1
 1

gave 10 for the first, and 1 for the second.  
(Now an option of the OPEN statement.)  The case
of all columns blanks still gives zero for,
I believe, all numeric input conversions.
(I believe including B, O, and Z, but have never
tried those.)

-- glen



Sun, 11 Sep 2011 03:54:52 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. beginner's question, differences among Smalltalk implementations

2. Standard or professional - what's the differences

3. What's the difference between bof() and eo()

4. What's The Difference ?

5. What's the difference between IO and File

6. What's the difference between Cosmo1.0 Player and Cosmo2.0 Player

7. What's The Difference ?

8. what's the difference?

9. What's the difference?

10. What's the difference between...

11. What's the difference between POINTER and ALLOCATABLE

12. What's the difference?

 

 
Powered by phpBB® Forum Software