在 Fortran90 中从文本文件中跳过一行

发布于 2024-10-31 04:27:47 字数 308 浏览 4 评论 0原文

我正在用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜味超标? 2024-11-07 04:27:47

一种可能的解决方案已经向您展示了,它使用“虚拟变量”,但我只是想补充一点,您甚至不需要虚拟变量,只需在进入循环之前一个空白的读取语句就足够了:

open(18, file='m3dv.dat')
read(18,*)
do
    ...

其他答案是正确的,但这可以提高代码的简洁性和(从而)可读性。

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:

open(18, file='m3dv.dat')
read(18,*)
do
    ...

The other answers are correct but this can improve conciseness and (thus) readability of your code.

孤城病女 2024-11-07 04:27:47

在 do 循环之前执行读取操作,将第一行上的任何内容读取到“虚拟”变量中。

program linereadtest
implicit none
character (LEN=75) ::firstline
integer :: temp,n
    !
    !
    !
open(18,file='linereadtest.txt')
read(18,*) firstline
do n=1,4
   read(18,'(i3)') temp
   write(*,*) temp
end do
stop
end program linereadtest

数据文件:

这是对 1000 件事的测试,其中 10
其中不存在

<前><代码>50
100
34
第566章

!忽略行和数字之间的空格,我无法将其格式化

Perform a read operation before the do loop that reads whatever is on the first line into a "dummy" variable.

program linereadtest
implicit none
character (LEN=75) ::firstline
integer :: temp,n
    !
    !
    !
open(18,file='linereadtest.txt')
read(18,*) firstline
do n=1,4
   read(18,'(i3)') temp
   write(*,*) temp
end do
stop
end program linereadtest

Datafile:

This is a test of 1000 things that 10
of which do not exist

50
100
34
566

!ignore the space in between the line and the numbers, I can't get it to format

生来就爱笑 2024-11-07 04:27:47
open (18, file='m3dv.dat')
open (19, file='m3dv2.dat')
read(18,*) x // <---

do
  read(18,*) x
  tmp = sqrt(x**2 + 1)
  write(19, *) tmp
end do

添加的行仅读取第一行,然后在第一次迭代时用第二行覆盖它。

open (18, file='m3dv.dat')
open (19, file='m3dv2.dat')
read(18,*) x // <---

do
  read(18,*) x
  tmp = sqrt(x**2 + 1)
  write(19, *) tmp
end do

The line added just reads the first line and then overwrites it with the seconde on the first iteration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文