在 Fortran90 中从文本文件中跳过一行
我正在用 fortran (90) 编写。我的程序必须读取 file1,对其每一行执行一些操作,然后将结果写入 file2。但问题是 file1 第一行有一些不需要的信息。
如何使用 Fortran 从输入文件中跳过一行?
代码:
open (18, file='m3dv.dat')
open (19, file='m3dv2.dat')
do
read(18,*) x
tmp = sqrt(x**2 + 1)
write(19, *) tmp
end do
第一行是文本和数字的组合。
I'm writing in fortran (90). My program must read file1, do something with every line of it and write result to file2. But the problem - file1 has some unneeded information in first line.
How can I skip a line from input file using Fortran?
The code:
open (18, file='m3dv.dat')
open (19, file='m3dv2.dat')
do
read(18,*) x
tmp = sqrt(x**2 + 1)
write(19, *) tmp
end do
First line is a combination of text and numbers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一种可能的解决方案已经向您展示了,它使用“虚拟变量”,但我只是想补充一点,您甚至不需要虚拟变量,只需在进入循环之前一个空白的读取语句就足够了:
其他答案是正确的,但这可以提高代码的简洁性和(从而)可读性。
One possible solution has already been presented to you which uses a "dummy variable", but I just wanted to add that you don't even need a dummy variable, just a blank read statement before entering the loop is enough:
The other answers are correct but this can improve conciseness and (thus) readability of your code.
在 do 循环之前执行读取操作,将第一行上的任何内容读取到“虚拟”变量中。
数据文件:
!忽略行和数字之间的空格,我无法将其格式化
Perform a read operation before the do loop that reads whatever is on the first line into a "dummy" variable.
Datafile:
!ignore the space in between the line and the numbers, I can't get it to format
添加的行仅读取第一行,然后在第一次迭代时用第二行覆盖它。
The line added just reads the first line and then overwrites it with the seconde on the first iteration.