I'm trying to read in a .gro file which looks like this:
1coh C 1 0.206 -0.049 0.429
1coh O1 2 0.295 0.048 0.501
1coh H3 3 0.252 -0.137 0.424
1coh H4 4 0.186 -0.014 0.337
I need to store the 0.206
, -0.049
, 0.429
into arrays.
I use this to recognise the coh
in the lines:
open(unit=20,file=grofile2)
do i=1,3
read(20,'(A)')line
lname2=line(6:8)
enddo
close(unit=20)
Then, read in the specific lines using:
open(unit=20,file=grofile2)
150 read(20,'(A)',end=151)line
if(line(6:8).eq.lname2)then
i=i+1
lig2(i)=line
write(*,*)lig2(i)
endif
goto 150
151 continue
close(unit=20)
natoms2=i
The write
here does output the lines to the terminal correctly.
I then follow this with:
do j=1,natoms2
write(*,*) j
write(*,*) lig2(j)
read(lig2(j)(23:28),*)lig2x(j)
read(lig2(j)(31:36),*)lig2y(j)
read(lig2(j)(39:44),*)lig2z(j)
write(*,*) lig2x(j),lig2y(j),lig2z(j)
enddo
to read the individual values to these arrays.
At this point I get the error:
At line 145 of file samgen.f
Fortran runtime error: Bad real number in item 1 of list input
Line 145 is:read(lig2(j)(23:28),*)lig2x(j)
This syntax has worked for this file in a different program, but is not working here. I've tried using F6.3
instead of *
in the read line. I've checked for variable declarations and types. What am I doing wrong? Any ideas? Or possibly a better way to read this in?
I take 'test.gro' file as an example.
1coh C 1 0.206 -0.049 0.429
1coh O1 2 0.295 0.048 0.501
1coh H3 3 0.252 -0.137 0.424
1coh H4 4 0.186 -0.014 0.337
"I need to store the 0.206, -0.049, 0.429 into arrays." I did not understand what follows in your question. Does this quite simple code do what you need?
program read
character :: char1*5, char2*3
integer :: i1
real :: r1, r2, r3
open (30, FILE='test.gro', STATUS='OLD')
do i=1,4
! I would read without format specification: it is much easier
! though type of variable must be correct!
read(30,*) char1, char2, i1, r1, r2,r3
!test what I have read:
print*, char1, char2, i1, r1, r2, r3
end do
close (30)
end program