【问题标题】:Write in an existing file without overwriting in Fortran在 Fortran 中写入现有文件而不覆盖
【发布时间】:2013-11-04 05:35:03
【问题描述】:

我有一个由 Fortran 程序(格式化)编写的现有文件,我想在文件开头添加几行。我们的想法是在不复制原始文件的情况下这样做。

我可以在文件末尾添加一行:

open(21,file=myfile.dat,status='old',action='write',
        form='formatted',position="append")
write(21,*) "a new line"

但是当我尝试时:

open(21,file=myfile.dat,status='old',action='write',
        form='formatted',position="rewind")
write(21,*) "a new line"

它会覆盖整个文件。

这可能是不可能的。 至少,我很高兴能确认这实际上是不可能的。

【问题讨论】:

    标签: fortran overwrite


    【解决方案1】:

    是的,这是不可能的。使用position=,您只需设置写入位置。通常,您只需通过写入顺序文件来删除当前记录之外的所有内容。您可能可以在直接访问文件的开头调整记录,但也不仅仅是在开头添加一些内容。您必须先制作副本。

    【讨论】:

    • 但是您不必一次将整个旧文件读入内存。使用操作系统将旧文件重命名为临时文件名。然后使用旧文件名创建一个新文件,并放入您想要的数据。然后将旧文件附加到新文件中。 (根据操作系统和文件数据的性质,您也许可以使用操作系统做到这一点。)
    • “先复制”也包括你的情况。
    【解决方案2】:

    如果您使用的是未格式化的数据并且知道需要多少行,请尝试使用直接访问文件读/写方法。这有可能将每一行的信息存储在“记录”中,以后可以像数组一样访问。

    为了追加到开头,只需在文件开头的“标题”中创建尽可能多的空记录,然后返回并将它们的值更改为您希望它们稍后出现的实际行.

    直接访问文件io示例:

    CHARACTER (20) NAME
    INTEGER I
    INQUIRE (IOLENGTH = LEN) NAME
    OPEN( 1, FILE = 'LIST', STATUS = 'REPLACE', ACCESS = 'DIRECT', &
             RECL = LEN )
    
    DO I = 1, 6
      READ*, NAME
      WRITE (1, REC = I) NAME             ! write to the file
    END DO
    
    DO I = 1, 6
      READ( 1, REC = I ) NAME             ! read them back
      PRINT*, NAME
    END DO
    
    WRITE (1, REC = 3) 'JOKER'            ! change the third record
    
    DO I = 1, 6
      READ( 1, REC = I ) NAME             ! read them back again
      PRINT*, NAME
    END DO
    
    CLOSE (1)
    END
    

    代码源,参见“直接访问文件”部分: http://oregonstate.edu/instruct/ch590/lessons/lesson7.html

    【讨论】:

      【解决方案3】:

      有可能!!!这是一个可以完成任务的示例程序。

         ! Program to write after the end line of a an existing data file  
         ! Written in fortran 90
         ! Packed with an example
      
        program write_end
        implicit none
        integer :: length=0,i
      
        ! Uncomment the below loop to check example 
        ! A file.dat is created for EXAMPLE defined to have some 10 number of lines
        ! 'file.dat may be the file under your concern'.
      
      
        !      open (unit = 100, file = 'file.dat')
        !      do i = 1,10
        !      write(100,'(i3,a)')i,'th line'
        !      end do
        !      close(100) 
      
        ! The below loop calculates the number of lines in the file 'file.dat'.
      
        open(unit = 110, file = 'file.dat' )
         do  
             read(110,*,end=10)
             length= length + 1 
         end do
         10   close(110)
      
        ! The number of lines are stored in length and printed.
      
         write(6,'(a,i3)')'number of lines= ', length
      
        ! Loop to reach the end of the file.
      
         open (unit= 120,file = 'file.dat')
         do i = 1,length
             read(120,*)
         end do
      
        ! Data is being written at the end of the file...
      
         write(120,*)'Written in the last line,:)'
         close(120)
         end
      

      【讨论】:

      • 他想写在文件的开头。对于写作,只需使用position=append
      • 谢谢,我没有正确阅读问题。永远不会再发生。我不知道 position = append, 但是 access='append', status = 'old' 之间是否有区别
      • access=append 是一些奇怪的非标准扩展。标准,Fortran 90 是position=append
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 2011-05-08
      • 2011-02-18
      • 2012-06-18
      • 2012-04-15
      • 1970-01-01
      • 2012-05-15
      相关资源
      最近更新 更多