gpt4 book ai didi

file-io - 使用 Fortran 90 正确读取输入文件中的注释行

转载 作者:行者123 更新时间:2023-12-02 17:17:10 26 4
gpt4 key购买 nike

据我了解,Fortran 在从文件读取数据时,会跳过以星号 (*) 开头的行,假设它们是注释。好吧,我似乎在用我创建的一个非常简单的程序实现这种行为时遇到了问题。这是我的简单 Fortran 程序:

  1       program test
2
3 integer dat1
4
5 open(unit=1,file="file.inp")
6
7 read(1,*) dat1
8
9
10 end program test

这是“file.inp”:

  1 *Hello
2 1

我用

构建了我的简单程序
gfortran -g -o test test.f90

当我运行时,我收到错误:

At line 7 of file test.f90 (unit = 1, file = 'file.inp')
Fortran runtime error: Bad integer for item 1 in list input

当我运行删除注释行的输入文件时,即:

1 1

代码运行良好。因此,Fortran 正确解释该注释行似乎存在问题。这一定是非常简单的事情,我在这里错过了,但我无法在谷歌上找到任何东西。

最佳答案

Fortran 不会自动跳过输入文件中的注释行。您可以通过首先将行读入字符串,检查注释符号的第一个字符或在字符串中搜索该符号,然后如果该行不是注释,则对字符串进行“内部读取”来轻松完成此操作获取数值。

类似于:

use, intrinsic :: iso_fortran_env

character (len=200) :: line
integer :: dat1, RetCode

read_loop: do
read (1, '(A)', isostat=RetCode) line
if ( RetCode == iostat_end) exit ReadLoop
if ( RetCode /= 0 ) then
... read error
exit read_loop
end if
if ( index (line, "*") /= 0 ) cycle read_loop
read (line, *) dat1
end do read_loop

关于file-io - 使用 Fortran 90 正确读取输入文件中的注释行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10259712/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com