user2914999
user2914999

Reputation: 11

Input conversion error in fortran

I want to read a file 20*4 dimention contains character and digital, and resort it out to another file, but I always have forrtl server(64): Input conversion error in fortran I will aprreciate if anyone can help me.

I have an input file a.txt (20*4) like this:

index    Ti    Te     Ne

1      2.3    2.5    0.6

2      2.9    3.2    0.8

3      3.4    3.6    1.1

.
.
.

20     7.3     8.9    3.5

My program is test.f90 like below:

program test

implicit none

real*8,allocatable  :: prof_Ne(:),prof_Te(:),prof_Ti(:)

integer :: i, j, n_skip, n_prof
character :: index*22

n_prof = 20

allocate(prof_Ne(n_prof), prof_Te(n_prof), prof_Ti(n_prof))

open(21,file='a.txt')

read(21,'(A25)') index

write(*,*) index

n_skip = 4
do i=1,n_skip
  read(21,*)
enddo

do i=1,n_prof
  read(21,'(i2,3e9.5)') j,prof_Ne(i),prof_Te(i),prof_Ti(i)
enddo
close(21)
write(*,*) prof_Ne

end program 

Upvotes: 1

Views: 6842

Answers (1)

Alexander Vogt
Alexander Vogt

Reputation: 18098

  • I wouldn't specify the format while reading in - this might lead to problems (as it does for you).
  • You read in the first 25 characters (the is more then the first line) into index - probably not what you want. Better use read(*,*) followed by four strings to let Fortran decided where to break those strings apart.
  • Then you skip four records - why?
  • Finally, you read 20 lines into the arrays - in your case beyond the end of the file! Again, you specify the format (which I wouldn't)...

My guess at what you are trying to achieve is below:

program test

  implicit none

  real*8,allocatable  :: prof_Ne(:),prof_Te(:),prof_Ti(:)

  integer :: i, j, n_prof
  character(len=22) :: index, dummy1, dummy2, dummy3

  n_prof = 20

  allocate(prof_Ne(n_prof), prof_Te(n_prof), prof_Ti(n_prof))

  open(21,file='a.txt')

  read(21,*) index, dummy1, dummy2, dummy3
  write(*,*) index, dummy1, dummy2, dummy3

  do i=1,n_prof
    read(21,*) j,prof_Ne(i),prof_Te(i),prof_Ti(i)
  enddo
  close(21)
  write(*,*) prof_Ne

end program 

Upvotes: 1

Related Questions