Home>

As an application of Fortran file operations

test
1,2,3
4,5
6,7,8

I want to read an array like this line by line, but it doesn't work.
The line count works fine, but it doesn't read the number, what should I do?

Execution result
n = 4
 test
   0.00000000
At line 27 of file p2.f90
Fortran runtime error: Attempting to allocate already allocated variable'a'
Error termination. Backtrace:
# 0 0xffffffffffffffff in ???
# 1 0xffffffffffffffff in ???
# 2 0xffffffffffffffff in ???
# 3 0xffffffffffffffff in ???
# 4 0xffffffffffffffff in ???
# 5 0xffffffffffffffff in ???
# 6 0xffffffffffffffff in ???
# 7 0xffffffffffffffff in ???
# 8 0xffffffffffffffff in ???
Corresponding source code
program p2
 implicit none
 real, allocatable :: a (:)
 integer i
 integer n
 character (len = 50) :: str1
open (20, file ='p2.txt', status ='old')
 read (20, *) str1
 n = 0
do
 read (20, *, end = 99) a
 n = n + 1
end do
99 print *, ('n ='), n
 print *, str1
do i = 2, n
 allocate (a (100))
 print *, a (i)
end do
close (20)
! open (21, file ='p2.csv', status ='replace')
! do i = 1, n
! write (21, *) a
! end do
! close (21)
end program p2
What I tried Supplementary information (FW/tool version, etc.)

Please provide more detailed information here.