Reputation: 11
I'm new to Fortran, and I want to parse a few lines to an array from
a big text file like below. After I read the complete file, I separated the
file into a few parts. And I want to parse specific lines into , L(i)
,
and write a few columns from lines. I tried to write a small part
of the program. I read all lines from the text file, but I don't know how
can I parse specific Line(i)
.
text file:
... ...
a b c d
1.2564E+2 0.2564E+2 1.25047E+2 3.2564E+1
1.2564E+2 0.2564E+2 1.25047E+2 3.2564E+1
1.2564E+2 0.2564E+2 1.25047E+2 3.2564E+1
1.2564E+2 0.2564E+2 1.25047E+2 3.2564E+1
..... .....
character*256 Line(155)
integer ierr, n, i, s
real:: a, b, c, d
open(10,file='b.txt', status='old')
do i=1,155
read(10,'(a)',iostat=ierr) Line(i)
if (ierr /= 0) exit
end do
close(10)
Upvotes: 1
Views: 295
Reputation: 261
You may read from a character variable using read
:
do i = 1, 155
read(line(i), *) a, b, c, d
print*,'a =',a
print*,'b =',b
print*,'c =',c
print*,'d =',d
enddo
If you are not planning to use Line
for anything else, you could have done that in the first place, reading a
, b
, c
and d
directly from your file.
Upvotes: 1