Error handling Fortran 90错误:输入需要太多数据
我有以下代码:Error handling Fortran 90错误:输入需要太多数据,error-handling,fortran,fortran90,Error Handling,Fortran,Fortran90,我有以下代码: if (complex) then read(unitvector) (CoefC(jl),jl=1,NV) endif 其中,用户指示数据是否为复数的集合。现在,如果用户指出是,但实际上不是,我得到错误67(输入需要太多数据)。我怎样才能捕捉到它,这样我就可以写出可能是用户犯了错误。我想它看起来会像: read(unitvector, ioStat=iocplx) (CoefC(jl),jl=1,NV) 但是我应该把“if”放在哪里来检查错误呢?这取决于程序的整体逻辑,我
if (complex) then
read(unitvector) (CoefC(jl),jl=1,NV)
endif
其中,用户指示数据是否为复数的集合。现在,如果用户指出是,但实际上不是,我得到错误67(输入需要太多数据)。我怎样才能捕捉到它,这样我就可以写出可能是用户犯了错误。我想它看起来会像:
read(unitvector, ioStat=iocplx) (CoefC(jl),jl=1,NV)
但是我应该把“if”放在哪里来检查错误呢?这取决于程序的整体逻辑,我们无法从这么小的代码片段中告诉您最好的方法。您可以尝试以下方法(未测试): 或
但是你真的没有指定你想要什么,停止程序并写一条有意义的消息?以其他方式读取数据?一切皆有可能,我们没有水晶球。没有回答您的问题,但更简单的读取语法是read(unitvector)CoefC(1:NV)。它确实有效,谢谢。谢谢。我只是不确定语法。由于read(unitvector)(CoefC(jl),jl=1,NV)类似于一个自我维持的循环,因此我不确定将IF语句粘贴到哪里,以便在程序崩溃之前首先执行它。你回答的第一部分就是我要找的。谢谢
if (complex) then
read(unitvector, ioStat=iocplx) (CoefC(jl),jl=1,NV)
if (iocplx/=0) stop "Error reading the complex data."
end if
if (complex) then
read(unitvector, ioStat=iocplx) (CoefC(jl),jl=1,NV)
if (iocplx/=0) then
write(*,*) "Error reading the complex data, triung real."
complex = .false.
backspace(unitvector)
read(unitvector, ioStat=ioreal) (CoefR(jl),jl=1,NV)
if (ioreal/=0) then
stop "Error reading real data."
end if
end if
end if