这是我的代码,没有编译,输出是这样的:
1>Error: The operation could not be completed
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========我使用微软视频工作室2019和英特尔并行工作室2020 fortran。
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我想读“3”,这就是为什么我忽略7个字符的原因。
发布于 2020-12-01 21:52:52
这只是一个纯粹的猜测,但在您的情况下,似乎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发布于 2020-12-02 01:53:45
当您想知道错误发生的原因时,通常最好包含错误消息。如果它没有打印在屏幕上,那么它应该在一些日志中。
尽管如此,我注意到了三件事,其中两件也被@evets和@Oo.oO注意到了:
7X的意思是“忽略7个字符”,但忽略不会读取值。现在我不知道输入文件是什么样子,也不知道为什么需要忽略前7个字符。一般来说,最好只是用读(单位,*)节点
但是,如果您确实需要声明格式,那么该格式说明符必须包含一些实际整数的组件,如下所示:
2格式(7X,I4)--这假定输入行中的第8到第11字符只包含数字和所有数字。后面的4 I表示要读取的数字包含多少个字符。
最后,
1I --在I之前有数字指示要读取多少整数。在这种情况下,1是多余的。但我相当肯定的是,I需要一个在I之后的数字来表示整数应该使用多少位数。现在,一些编译器似乎接受I0的意思,意思是“任意数量”,但我不知道这是哪个标准,以及编译器是否接受它。有些编译器也可能只接受I,但我认为这是不符合标准的。(我肯定有人会在下面的评论中纠正我; )
干杯
发布于 2020-12-02 07:48:38
谢谢你的帮助。现在,根据您的建议,我以如下方式更改了代码:
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: 3https://stackoverflow.com/questions/65097502
复制相似问题