Fortran Coder

标题: Fortran读取文件问题 [打印本页]

作者: 哈哈哈超开心    时间: 2022-9-18 15:01
标题: Fortran读取文件问题
各位大神好!
       我想试着从一个文件里读取我想要的内容,比如“ 11 22 33 ” “20 23 55”这种特定格式的两行,我就这样用了read语句,代码如下。但是却报错input conversion error,请问怎么解决呢? 如何跳过那些不是这个1002格式的行,只提取符合这个格式的行呢? 感谢!
文件是: 11 22 33
              helloworld
              35 336 111
              20 23 55
              #2022
代码是:
      read(11,1002) num1,num2,num3
1002 format(i2,1x,i2,1x,i2)




作者: fcode    时间: 2022-9-19 08:16
read(11,1002) num1,num2,num3
改为
read(11,*) num1,num2,num3


在99.99%的情况下,read不需要指定格式。表控是最佳的选择。
作者: fcode    时间: 2022-9-19 08:17
跳过格式不符的行
[Fortran] 纯文本查看 复制代码
Do
  read(11,*,ioStat=k) num1 , num2 , num3
  if(k/=0) cycle
End Do

作者: 哈哈哈超开心    时间: 2022-9-19 15:03
fcode 发表于 2022-9-19 08:17
跳过格式不符的行
[mw_shl_code=fortran,true]Do
  read(11,*,ioStat=k) num1 , num2 , num3

好的感谢!我去实践一下!




欢迎光临 Fortran Coder (http://bbs.fcode.cn/) Powered by Discuz! X3.2