用fortran覆盖文件 [英] overwrite a file using fortran
问题描述
我正在使用写入文件的Fortran 90程序。这个文件的第一行应该表示剩余文件中的行数。当某个标准得到满足并且不能事先确定时,该文件由程序编写。基本上,只有在运行结束后我才能知道总数。
我想按照以下方式来完成:
<1>打开文件并在第一行写上一些文字说 Hello
<2>写入根据需要在文件中保留行,并为行数保留一个 counter 。
3)运行结束后, ,请使用计数器替换第一行( Hello )。
。我不知道如何替换第一行。我能想到的另一个选择是写入2个文件。首先,在没有计数器的情况下写上面的文件。一旦运行结束,关闭文件并写入另一个文件,这一次,我知道计数器的值。
我相信有一种方法可以继续第一种方法。有人可以帮我解决这个问题吗? 解决方案
返回顺序访问文件非常棘手,因为行的长度可能会有所不同。如果你改变了一行的长度,你必须将所有的东西都放在后面。
我推荐的是将你的输出写入一个临时文件,同时计数行数。然后,一旦完成后,倒回暂存文件,将行数写入输出文件,并将暂存文件的内容复制到该输出文件。
以下是我所做的:
程序var_file
隐式无
字符(len = *),参数:: filename ='delme.dat'
integer :: n,io_stat
字符(len = 300):: line
open(unit = 200,status ='SCRATCH ',action =READWRITE)
n = 0
do
read(*,'(A)')line
if(len_trim行)== 0)退出!空行 - >完成
n = n + 1
写入(200,'(A)')修整(行)
结束做
倒带(200)
open(unit = 100,file = filename,status =unknown,action =write)
write(100,'(I0)')n
do
read(200,'(A)',iostat = io_stat)line
if(io_stat / = 0)exit
write(100,'(A)')trim(line)
end do
close(200)
close(100)
end program var_file
I am using a Fortran 90 program that writes a file. The first line of this file is supposed to indicate the number of lines in the remaining file. The file is written by the program when a certain criterion is met and that can't be determined beforehand. Basically, I will know the total number of lines only after the run is over.
I want to do it in the following manner:
1) open the file and write the first line with some text say, "Hello"
2) Write rows in the file as desired and keep a counter for number of rows.
3) Once the run is over and just before closing the file, replace the first line string ("Hello") with the counter.
The problem is in step 3. I don't know how to replace the first line.
Another option that I can think of is to write to 2 files. First, write a file as above without the counter. Once the run is over, close the file and write another file and this time, I know the value of the counter.
I believe there is a way to proceed with the first approach. Can someone please help me with this?
Going back on a sequential access file is tricky, because lines can vary in length. And if you change the length of one line, you'd have to move all the stuff behind.
What I recommend is to write your output to a scratch file while counting the number of lines. Then, once you're finished, rewind the scratch file, write the number of lines to your output file, and copy the contents of the scratch file to that output file.
Here's what I did:
program var_file
implicit none
character(len=*), parameter :: filename = 'delme.dat'
integer :: n, io_stat
character(len=300) :: line
open(unit=200, status='SCRATCH', action="READWRITE")
n = 0
do
read(*, '(A)') line
if (len_trim(line) == 0) exit ! Empty line -> finished
n = n + 1
write(200, '(A)') trim(line)
end do
rewind(200)
open(unit=100, file=filename, status="unknown", action="write")
write(100, '(I0)') n
do
read(200, '(A)', iostat=io_stat) line
if (io_stat /= 0) exit
write(100, '(A)') trim(line)
end do
close(200)
close(100)
end program var_file
这篇关于用fortran覆盖文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!