为什么我的代码没有用 Fortran 编译?
Why my code doesn't get compiled in fortran?
这是我的代码,它没有被编译
输出就是这样:
1>Error: The operation could not be completed
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
我正在为 Fortran 使用 Microsoft visual studio 2019 和 intel parallel studio 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 的单元号。其中一些可能保留用于标准输入和输出,或错误输出,或类似的东西。
您尝试读取一个整数,但您提供的格式不包含任何整数描述符。 7X
仅表示“忽略 7 个字符”,但忽略不会读取值。现在我不知道您的输入文件是什么样的,或者为什么您觉得需要忽略前 7 个字符。通常最好只使用
read(unit, *) Nnodes
但是如果您确实需要声明格式,那么该格式说明符必须包含一些实际整数的组成部分,如下所示:
2 FORMAT(7X, I4)
这假设输入行中的第 8 到第 11 个字符只包含数字,并且包含所有数字。 I
后面的4
表示要读取的数字包含多少个字符。
最后是打印语句的格式。您有 1I
-- I
之前的数字表示要读取多少个整数。 1
在那种情况下是多余的。但我有理由确定 I
在 之后需要一个数字 I
来表示整数应该使用多少位数字。
现在有些编译器似乎接受I0
意思是'just as many as you need',但我不知道那是哪个标准,不知道你的编译器是否接受。一些编译器也可能只接受 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
这是我的代码,它没有被编译 输出就是这样:
1>Error: The operation could not be completed
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
我正在为 Fortran 使用 Microsoft visual studio 2019 和 intel parallel studio 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 的单元号。其中一些可能保留用于标准输入和输出,或错误输出,或类似的东西。
您尝试读取一个整数,但您提供的格式不包含任何整数描述符。
7X
仅表示“忽略 7 个字符”,但忽略不会读取值。现在我不知道您的输入文件是什么样的,或者为什么您觉得需要忽略前 7 个字符。通常最好只使用read(unit, *) Nnodes
但是如果您确实需要声明格式,那么该格式说明符必须包含一些实际整数的组成部分,如下所示:
2 FORMAT(7X, I4)
这假设输入行中的第 8 到第 11 个字符只包含数字,并且包含所有数字。
I
后面的4
表示要读取的数字包含多少个字符。最后是打印语句的格式。您有
1I
--I
之前的数字表示要读取多少个整数。1
在那种情况下是多余的。但我有理由确定I
在 之后需要一个数字I
来表示整数应该使用多少位数字。现在有些编译器似乎接受
I0
意思是'just as many as you need',但我不知道那是哪个标准,不知道你的编译器是否接受。一些编译器也可能只接受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