Reputation: 395
How can I change this code for it to work?
real(8), dimension(11), intent(inout) :: x
integer(4) :: idx
character(len=4) :: fake_read_first
character(len=4) :: fake_read_second
integer(4) :: fake_index
do idx = 1_idp, n, 1_idp
read(nfile, FMT="(a,i5,a,f28.18)") fake_read_first, fake_index, &
fake_read_second, x(idx) ! read line wise
end do
It reads data on a file that has the following form (with spaces as it is)
eps( 1) = -1.534875443410773865
eps( 2) = 1.879729927704710146
eps( 3) = -1.141802880301361789
eps( 4) = -0.762516225079661325
eps( 5) = -0.698569538839495241
eps( 6) = 1.000000000000000000
eps( 7) = 1.918166206055258449
eps( 8) = 0.321720085182322979
eps( 9) = -1.325650752925679132
eps( 10) = -1.596179600738307780
eps( 11) = 1.786605261597484340
I am getting the following error:
At line 90 of file ddnls_mlce_thps_main.f90 (unit = 10, file =
'__init_ddnls_mlce_thps__.ini')
Fortran runtime error: Bad value during integer read
Upvotes: 1
Views: 155
Reputation: 8140
First things first: Your code seems to work. Check whether your data file conforms to the format you've given us on every line.
Secondly: There are many ways to read such data. You can do what you did and use dummy variables that get discarded after each read.
What I would do, because I think it makes things easy to understand in the code (which I think is very valuable) is to read each line into a string, find the =
in that string, then read the values out of that string. Here's an example:
program read_data
use iso_fortran_env, only: int32, real64
implicit none
real(kind=real64) :: x(11)
character(len=100) :: line
integer(kind=int32) :: idx, start_pos
integer :: u
open(newunit=u, file='data.txt', action='read', status='old')
do idx = 1, size(x), 1
read(u, '(A)') line
start_pos = index(line, '=') + 1
read(line(start_pos:), *) x(idx)
end do
close(u)
print '(F24.18)', x
end program read_data
You might want to introduce some error checking: Check the iostat
and iomsg
values from the open
and read
statements. Check whether there is actually an =
sign in the line by checking whether start_pos
is larger than 1 (index
returns 0 if the substring is not found).
Upvotes: 3