【问题标题】:Fortran 90 Error: input requires too much dataFortran 90 错误:输入需要太多数据
【发布时间】:2014-04-01 15:54:59
【问题描述】:

我有以下代码:

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)
  • 它确实有效,谢谢。

标签: error-handling fortran fortran90


【解决方案1】:

这取决于程序的整体逻辑,我们无法从这么小的代码sn-p告诉你最好的方法。您可以尝试类似(未测试):

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

但是你真的没有指定你想要什么,停止程序并写一个有意义的消息?以其他方式读取数据?一切皆有可能,我们没有水晶球。

【讨论】:

  • 谢谢。我只是不确定语法。由于 read(unitvector) (CoefC(jl),jl=1,NV) 就像一个自我维持的循环,我不确定在哪里粘贴 IF 语句,因为它在程序崩溃之前首先执行。您答案的第一部分是我正在寻找的。谢谢。
猜你喜欢
  • 2015-08-07
  • 2014-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-28
  • 2013-04-20
  • 2013-06-09
  • 2012-01-20
相关资源
最近更新 更多