如何在Fortran中将数据写入文件中的特定行?

发布于 2024-11-30 01:37:30 字数 115 浏览 1 评论 0原文

我有无序数据,我想将此数据写入有顺序的文件中。例如,该值的顺序是第 70 行,则将该值写入文件中的第 70 行。如何向前移动文件指针?

有没有像BACKSPACE这样的命令?

I have unordered data and I want to write this data to a file with an order. For example, the value's order is 70th, then this value is written to 70th line in the file. How can I move file pointer forward?

Is there any command like BACKSPACE?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

久伴你 2024-12-07 01:37:30

当您谈论行和退格键时,大概您的意思是 access="sequential" 和 form="formatted"。

在这种情况下,为了向前跳过记录,您可以只进行空读取,例如

read(unitnum, *)

As you're talking about lines and backspace, presumably you mean access="sequential" and form="formatted".

In that case, in order to skip a record forward, you can do just an empty read, e.g.

read(unitnum, *)
星光不落少年眉 2024-12-07 01:37:30

您还可以使用直接访问文件,其中可以不按顺序写入和读取记录。例如,请参见 http://en.wikipedia.org/wiki/Fortran_95_language_features#Direct- access_files

一天后编辑:

已建议使用顺序文件的解决方案。我认为这些不会起作用......如果您知道如何使其起作用,请解释一下。 (当然,你可以对内存中的值进行排序,然后按顺序写出。)下面是一些示例代码来说明问题。它创建一个 10 行的文件,然后假设您要写入第 5 个值:

program test_rewind

   integer :: i, j


   open (unit=15, file="test_rewind.txt", access="sequential", form="formatted", action="readwrite" )

   do i=1,10
      write (15, '(I4)') i
   end do

   rewind (15)

   do i=1,4
      read (15, *) j
   end do

   write (15, '(I4)') 99

   stop

end program test_rewind

输出文件包含:

   1
   2
   3
   4
  99

顺序文件的问题是写入现有文件会擦除该点之后的所有内容。

与直接访问解决方案相比:

program test_rewind

   integer :: i

   open (unit=15, file="test_rewind.dat", access="direct", form="unformatted", action="readwrite", recl=4 )

   do i=1,10
      write (15, rec=i) i
   end do

   write (15, rec=5) 99

   stop

end program test_rewind

更短并且有效 - 输出文件包含十个数字,其中第五个数字从 5 更改为 99。但是,它们是二进制的。

You can also use a direct access file, in which the records can be written and read out of order. See, for example, http://en.wikipedia.org/wiki/Fortran_95_language_features#Direct-access_files

Edit after one day:

Solutions using a sequential file have been suggested. I don't think these will work ... please explain if you know how to make it work. (Of course, you can sort the values in memory and write them out sequentially.) Here is some sample code to illustrate the problem. It creates a file of 10 lines, then supposes that you want to write the 5th value:

program test_rewind

   integer :: i, j


   open (unit=15, file="test_rewind.txt", access="sequential", form="formatted", action="readwrite" )

   do i=1,10
      write (15, '(I4)') i
   end do

   rewind (15)

   do i=1,4
      read (15, *) j
   end do

   write (15, '(I4)') 99

   stop

end program test_rewind

The output file contains:

   1
   2
   3
   4
  99

The problem for the sequential file is that a write to an existing file erases everything after that point.

Compare to the direct access solution:

program test_rewind

   integer :: i

   open (unit=15, file="test_rewind.dat", access="direct", form="unformatted", action="readwrite", recl=4 )

   do i=1,10
      write (15, rec=i) i
   end do

   write (15, rec=5) 99

   stop

end program test_rewind

Shorter and it works -- the output file contains ten numbers with the 5th changed from 5 to 99. However, they are binary.

城歌 2024-12-07 01:37:30

对于每个数据条目,使用 janneb 描述的方法到达所需的行。然后使用 REWIND 语句返回到文件的开头(仅限 access='sequential')。

另外,如果您需要它,请查找格式描述符以了解如何沿单行向左/向右移动。

For each data entry, use method described by janneb to get to a desired line. Then use REWIND statement to go back to the beginning of the file (access='sequential' only).

Also in case you need it, look up format descriptors to see how to move left/right along a single line.

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