为什么我的代码没有在 fortran 中编译?



这是我的代码,它不会被编译输出就是这样:

1>Error: The operation could not be completed 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我正在为fortran使用微软visual studio 2019和英特尔并行工作室2020。

program Truss
implicit none
integer :: Nnodes
open(unit=1,file='data.txt')
open(unit=2,file='output.txt')
read(1,2) Nnodes
write(2,3) Nnodes
2  format(7x)
3  format("Number of nodes:",1I)

end program Truss

文件data.txt包含以下内容:

npoint 3
0 0
2 0
1 1

我想读npoint后面的"3",这就是为什么我忽略了7个字符。

这只是一个纯粹的猜测,但在您的情况下,7x后面应该跟着类型规范(例如I1(。

program Truss
implicit none
integer :: Nnodes
open (unit=10, file='data.txt')
open (unit=20, file='output.txt')
read (10, 2) Nnodes
write(20, 3) Nnodes
2 format(7X,I1)
3 format("Number of nodes: ", I1)
end program Truss

但是,正如我所说,这只是猜测你想要实现什么。

我假设您的输入文件如下所示:

> cat data.txt
1

当您想知道错误发生的原因时,通常最好包含错误消息。如果它没有打印在屏幕上,那么它应该在一些日志中。

也就是说,我注意到了三件事,其中两件也被@evets和@Oo.Oo注意到了:

  • 请勿使用低于10的单位编号。其中一些可能是为标准输入和输出、错误输出或类似的东西保留的。

  • 您试图读取一个整数,但所提供的格式不包含任何整数描述符。CCD_ 3只是表示";忽略7个字符";,但忽略不会读取值。现在我不知道你的输入文件是什么样子的,也不知道为什么你觉得有必要忽略前7个字符。一般来说,最好只使用

    read(unit, *) Nnodes
    

    但是,如果您确实需要声明格式,那么格式说明符必须包含实际整数的某些组件,如以下所示:

    2 FORMAT(7X, I4)  
    

    这假设输入行中的第8个到第11个字符只包含数字,并且全部包含数字。I之后的4表示要读取的数字包含多少个字符。

  • 最后,还有print语句的格式。您有1I——I之前的数字表示要读取的整数数量。在这种情况下CCD_ 8是多余的。但我有理由确信,I需要在I之后有一个数字来表示整数应该使用多少位数。

    现在有些编译器似乎接受I0,意思是"你需要多少就有多少",但我不知道这是哪个标准,也不知道你的编译器是否接受它。有些编译器可能只接受I,但我认为这不符合标准。(我相信有人会在下面的评论中纠正我的答案;(

干杯

感谢您的帮助。现在根据你的建议,我把代码改成了这样:

program Truss
implicit none
integer :: Nnodes
open(unit=11,file='data.txt')
open(unit=22,file='output.txt')
read(11,20) Nnodes
write(22,30) Nnodes
20  format(7x,I3)
30  format("Number of nodes:",2I)

end program Truss

现在我想忽略2行,并根据格式30在output.txt中写入数据,但它就是这样做的:

Number of nodes:           3

最新更新