From: Hash on
Hello,

I'm trying to read the next data text file :
$ cat test.dat
1.0000000e+00
2.0000000e+00
3.7000000e+09
3.0000000e+17
1.5000000e+20
5.7000000e+01
7.0000000e-02
....

These integer are written as they were real (and I can't change the
way it is). However, I would like to read these as integers, not as
reals. I'm a beginner in fortran, and I tried the code below :

----------test.f90------------------------
program test
Integer :: Nmh, Nme

open(unit=1, file='test.dat', status='old', form='formatted')

read(1,*) Nmh
read(1,*) Nme
write(*,*) Nmh, Nme
end program test
----------------------------------

The problem is on the reading step, where I obtain the following
error :

$ ./test
At line 6 of file test.f90
Fortran runtime error: Bad integer for item 1 in list input

Of course, I could read the file with Real variables, then cast it to
Integer.
But, is there a better way to do it ?

Thanks in advance for your advices.

JH
From: dpb on
Hash wrote:
....
> I'm trying to read the next data text file :
> $ cat test.dat
> 1.0000000e+00
> 2.0000000e+00
> 3.7000000e+09
> 3.0000000e+17
> 1.5000000e+20
> 5.7000000e+01
> 7.0000000e-02
> ....
>
> These integer are written as they were real (and I can't change the
> way it is). However, I would like to read these as integers, not as
> reals. I'm a beginner in fortran, and I tried the code below :
>
> ----------test.f90------------------------
> program test
> Integer :: Nmh, Nme
>
> open(unit=1, file='test.dat', status='old', form='formatted')
>
> read(1,*) Nmh
> read(1,*) Nme
....
> At line 6 of file test.f90
> Fortran runtime error: Bad integer for item 1 in list input
>
> Of course, I could read the file with Real variables, then cast it to
> Integer.
....

Not really. There are at least some (or should I say one?) compiler(s)
that implement an extension on list-directed input to do an implicit
type conversion, but it is that--an extension so you would have to have
a compiler with that specific extension implemented to accomplish the task.

In general, the correct way is to read into a variable of proper type
and do a conversion as desired.

I note that while you didn't try to read past the first two items, at
least some of the following values either are larger than a default
integer would hold w/o overflow or are actual floating point values so
they would have to be held in either a larger integer or as floating
point anyway.

--
From: John Harper on
In article <fu89u6$bft$1(a)aioe.org>, dpb <none(a)non.net> wrote:
>Hash wrote:
>...
>> I'm trying to read the next data text file :
>> $ cat test.dat
>> 1.0000000e+00
>> 2.0000000e+00
>> 3.7000000e+09
>> 3.0000000e+17
>> 1.5000000e+20
>> 5.7000000e+01
>> 7.0000000e-02
>> ....
>>
>> These integer are written as they were real (and I can't change the
>> way it is). However, I would like to read these as integers, not as
>> reals. I'm a beginner in fortran, and I tried the code below :
>>
>> ----------test.f90------------------------
>> program test
>> Integer :: Nmh, Nme
>>
>> open(unit=1, file='test.dat', status='old', form='formatted')
>>
>> read(1,*) Nmh
>> read(1,*) Nme
>...
>> At line 6 of file test.f90
>> Fortran runtime error: Bad integer for item 1 in list input
>>
>> Of course, I could read the file with Real variables, then cast it to
>> Integer.
>...
>
>Not really. There are at least some (or should I say one?) compiler(s)
>that implement an extension on list-directed input to do an implicit
>type conversion, but it is that--an extension so you would have to have
>a compiler with that specific extension implemented to accomplish the task.
>
>In general, the correct way is to read into a variable of proper type
>and do a conversion as desired.

There is a useful exception, which is the opposite of what dpb was
trying to do. In * format you may read an integer in the data into a
real variable, e.g. 200 would be read as if it were 200.0 or 2.00e+2.

>I note that while you didn't try to read past the first two items, at
>least some of the following values either are larger than a default
>integer would hold w/o overflow or are actual floating point values so
>they would have to be held in either a larger integer or as floating
>point anyway.

Even with no overflow, a default real variable may not be able to hold
an integer value to the necessary precision, e.g.

PROGRAM testread1
CHARACTER:: c*15 = '1.234567890e+09'
REAL :: x
INTEGER :: i
READ(c,*)x
i = x
WRITE(*,'(2A)')' c = ',c
WRITE(*,'(3(A,I0))')' i = ',i,' huge(i) = ',huge(i), &
' nint(x*epsilon(x)) = ',nint(x*epsilon(x))
END PROGRAM testread1

That gave me the following output:

c = 1.234567890e+09
i = 1234567936 huge(i) = 2147483647 nint(x*epsilon(x)) = 147

Declaring x as DOUBLE PRECISION would fix that problem but not the
overflow possibility. If your huge(i) is the same as mine, try changing
e+09 to e+10 in the initialization of c.

-- John Harper, School of Mathematics, Statistics and Computer Science,
Victoria University, PO Box 600, Wellington 6140, New Zealand
e-mail john.harper(a)vuw.ac.nz phone (+64)(4)463 6780 fax (+64)(4)463 5045
From: Hash on
On 17 avr, 21:53, dpb <n...(a)non.net> wrote:
> Hash wrote:
>
> ...
>
> > I'm trying to read the next data text file :
> > $ cat test.dat
> > 1.0000000e+00
> > 2.0000000e+00
> > 3.7000000e+09
> > 3.0000000e+17
> > 1.5000000e+20
> > 5.7000000e+01
> > 7.0000000e-02
> > ....
>
> > These integer are written as they were real (and I can't change the
> > way it is). However, I would like to read these as integers, not as
> > reals. I'm a beginner in fortran, and I tried the code below :
>
> > ----------test.f90------------------------
> > program test
> > Integer :: Nmh, Nme
>
> > open(unit=1, file='test.dat', status='old', form='formatted')
>
> > read(1,*) Nmh
> > read(1,*) Nme
> ...
> > At line 6 of file test.f90
> > Fortran runtime error: Bad integer for item 1 in list input
>
> > Of course, I could read the file with Real variables, then cast it to
> > Integer.
>
> ...
>
> Not really. There are at least some (or should I say one?) compiler(s)
> that implement an extension on list-directed input to do an implicit
> type conversion, but it is that--an extension so you would have to have
> a compiler with that specific extension implemented to accomplish the task.
>
> In general, the correct way is to read into a variable of proper type
> and do a conversion as desired.
>
> I note that while you didn't try to read past the first two items, at
> least some of the following values either are larger than a default
> integer would hold w/o overflow or are actual floating point values so
> they would have to be held in either a larger integer or as floating
> point anyway.
>
> --

OK.

Thanks all for your answers.
From: see_address on
On Thu, 17 Apr 2008 12:40:26 -0700 (PDT), Hash
<julien.hillairet(a)gmail.com> wrote:

>Hello,
>
>I'm trying to read the next data text file :
>$ cat test.dat
> 1.0000000e+00
> 2.0000000e+00
> 3.7000000e+09
> 3.0000000e+17
> 1.5000000e+20
> 5.7000000e+01
> 7.0000000e-02
> ....
>
>These integer are written as they were real (and I can't change the
>way it is). However, I would like to read these as integers, not as
>reals. I'm a beginner in fortran, and I tried the code below :
>
>----------test.f90------------------------
>program test
> Integer :: Nmh, Nme
>
> open(unit=1, file='test.dat', status='old', form='formatted')
>
> read(1,*) Nmh
> read(1,*) Nme
> write(*,*) Nmh, Nme
>end program test
>----------------------------------
>
>The problem is on the reading step, where I obtain the following
>error :
>
>$ ./test
>At line 6 of file test.f90
>Fortran runtime error: Bad integer for item 1 in list input
>
>Of course, I could read the file with Real variables, then cast it to
>Integer.
>But, is there a better way to do it ?
>
>Thanks in advance for your advices.
>
>JH
There are routines (FORTRAN 77 and Fortran 95) at
http://www.nndc.bnl.gov/nndcscr/ensdf_pgm/nsdflib/
that will read information as a string and perform the conversion.
--
Jeff Ryman
email: rymanjc_at_yahoo_dot_com
 | 
Pages: 1
Prev: cvf66c: building program
Next: a cute program