io - How are input lines defined for I/0 statements? -
i want read values file called input.txt. input file looks this:
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 .... in total 36 lines of 1,2,3 , 4s
then use following code read file:
program outputtest implicit none double precision, allocatable, dimension(:) :: a,b,c,d integer :: counter,ii, ierror ierror=0 counter=1 open(39, action='read', name='input.txt', status='old') read(39,100, iostat=ierror) 100 format(t8,i1) if (ierror>0) print *, 'error when reading' stop end if if (ierror<0) print*, 'end of file reached' exit end if counter=counter+1 end if (.not.allocated(a)) allocate(a(counter)) if (.not.allocated(b)) allocate(b(counter)) if (.not.allocated(c)) allocate(c(counter)) if (.not.allocated(d)) allocate(d(counter)) ii=1, counter-1 read(39,110, iostat=ierror) a(ii), b(ii), c(ii), d(ii) 110 format(t8, i4, t16, i4, t24, i4, t32, i4) if (ierror.neqv.0) exit end close(39) write (*,120) write (*,120) b write (*,120) c write (*,120) d 120 format('result:', 1x,i2) end program outputtest the problem a,b,c,d equal zero, i.e. output is:
result: 0 result: 0 result: 0 .... this means not read either correct lines or make mistake. used in vim option ruler see lines entries in , used lines t. created file input.txt using tabs. mistake?
a 2 things:
- you don't
rewindin file. @ first read through whole file number of lines, without rewind, start reading @ end -- means nothing gets read arrays. readexplicit formats dangerous. have input file matches format precisely. in case, i'd @high-performance-mark suggested , usefmt=*. input file should read automatically.
that's can gleam quick browse. see if helps.
Comments
Post a Comment