哈哈哈超开心 发表于 2022-9-18 15:01:08

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:35

read(11,1002) num1,num2,num3
改为
read(11,*) num1,num2,num3


在99.99%的情况下,read不需要指定格式。表控是最佳的选择。

fcode 发表于 2022-9-19 08:17:58

跳过格式不符的行
Do
read(11,*,ioStat=k) num1 , num2 , num3
if(k/=0) cycle
End Do

哈哈哈超开心 发表于 2022-9-19 15:03:11

fcode 发表于 2022-9-19 08:17
跳过格式不符的行
Do
read(11,*,ioStat=k) num1 , num2 , num3


好的感谢!我去实践一下!
页: [1]
查看完整版本: Fortran读取文件问题