hpa 发表于 2020-2-21 14:55:32

运行后出现runtime error

麻烦高手们帮我看一下,运行后出现Fortran runtime error,不知道哪里出错了
Program main
Integer,parameter :: length=1000
integer i
real d
Character(LEN=length) A,B,Filename
Integer(kind=4)::Column, Row, K,L
real , allocatable :: c( : , : )
Column=0;Row=0;K=0;L=1;filename="ztzl-1.txt"
Open (55,file=Filename)
Read(55,'(A)') A
Do I=1, length
If(A(I:I)==','.or.A(I:I)==' ') then
    If(I-1>=L)Column=Column+1
    L=I+1
End if
End do
CLOSE(55)
Open (55,file=Filename)
Do
Read(55,*,iostat=k) B
   If( k /= 0 ) Exit
       Row = Row + 1
End Do
Write(*,"(A5,I6,5X,A7,I2)") "Row=",Row,"Column=",Column!读取行数
allocate(c(15,row))
Do i = 1,Row
read(55,*) d,c(:,1)
End Do
Close(55)!将数据转化为数组
end

fcode 发表于 2020-2-21 17:14:54

请给出输入文件,以便判断问题。

fcode 发表于 2020-2-21 17:19:42

试试
allocate(c(15,row))
改为
allocate(c(column-1,row))
rewind(55)

hpa 发表于 2020-2-22 09:48:16

fcode 发表于 2020-2-21 17:14
请给出输入文件,以便判断问题。

您好,这是文件

hpa 发表于 2020-2-22 10:12:22

fcode 发表于 2020-2-21 17:19
试试
allocate(c(15,row))
改为


而且我这个中间是用tab间隔符隔开的,他的列应该是有15列,但是输出来是1

fcode 发表于 2020-2-22 13:27:53

Program main
Integer,parameter :: length=1000
integer i
real d
Character(LEN=length) A,B,Filename
Integer(kind=4)::Column, Row, K,L
real , allocatable :: c( : , : )
Column=0;Row=0;K=0;L=1;filename="ztzl-1.txt"
Open (55,file=Filename)
Read(55,'(a1000)') A
column=GetDataN(A)!//改成这个函数,具体实现在下面的代码
CLOSE(55)
Open (55,file=Filename)
Do
    Read(55,*,iostat=k) B
    If( k /= 0 ) Exit
    Row = Row + 1
End Do
Write(*,"(A5,I6,5X,A7,I2)") "Row=",Row,"Column=",Column!读取行数
allocate(c(column-1,row))
rewind(55)
Do i = 1,Row
    read(55,*) d,c(:,i)!//1改成 i
End Do
Close(55)!将数据转化为数组

contains

Integer Function GetDataN( cStr )
    Character( Len = * ) , Intent( IN ) :: cStr
    Integer :: i
    Logical :: bIsSeparator , bIsQuote
    GetDataN = 0
    bIsSeparator = .TRUE.
    bIsQuote = .FALSE.
    Do i = 1 , Len_Trim( cStr )
      Select Case( cStr(i:i) )
      Case( '"' , "'" ) !// 如果遇到引号
      If ( .Not.bIsQuote ) GetDataN = GetDataN + 1!//如果不在引号中,则增加一个数据
      bIsQuote = .Not.bIsQuote !// 引号结束或开始
      bIsSeparator = .FALSE.
      Case( " " , "," , char(9) ) !// 如果遇到分隔符
      If ( .Not.bIsQuote ) then!// 分隔符如果不在引号中
          bIsSeparator = .TRUE.
      End If
      Case Default      
      If ( bIsSeparator ) then
          GetDataN = GetDataN + 1
      End If
      bIsSeparator = .FALSE.
      End Select
    End Do
End Function GetDataN

End Program Main

hpa 发表于 2020-2-22 14:02:37

fcode 发表于 2020-2-22 13:27
Program main
Integer,parameter :: length=1000
integer i


太谢谢您了
页: [1]
查看完整版本: 运行后出现runtime error