Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Fortran从某一行读取数据,并在同一文件的末尾写入数据?_Fortran - Fatal编程技术网

如何使用Fortran从某一行读取数据,并在同一文件的末尾写入数据?

如何使用Fortran从某一行读取数据,并在同一文件的末尾写入数据?,fortran,Fortran,我有一个如下所示的文件: -7307.5702506795660 -13000.895251555605 -11777.655135862333 0.52503289678626652 0.51683849096298218 31.160950279498426 -7307.5698242187500 -13000.900390625000 -11777.658203125000 -

我有一个如下所示的文件:

 -7307.5702506795660       -13000.895251555605       -11777.655135862333       0.52503289678626652       0.51683849096298218        31.160950279498426       -7307.5698242187500       -13000.900390625000       -11777.658203125000     
  -7307.5712457548034       -13000.883260393683       -11777.647978916109       0.52714817702425010       0.84740489721298218        20.800333023071289       -7307.5698242187500       -13000.900390625000       -11777.658203125000
   open(1,file='my_file.txt',status='old')
   do
    read(1,*,end=10) xe,ye,ze,the,phe,enel,x0,y0,z0
    ...some mathematical calculations
   end do
10 close(1)
我是这样读的:

 -7307.5702506795660       -13000.895251555605       -11777.655135862333       0.52503289678626652       0.51683849096298218        31.160950279498426       -7307.5698242187500       -13000.900390625000       -11777.658203125000     
  -7307.5712457548034       -13000.883260393683       -11777.647978916109       0.52714817702425010       0.84740489721298218        20.800333023071289       -7307.5698242187500       -13000.900390625000       -11777.658203125000
   open(1,file='my_file.txt',status='old')
   do
    read(1,*,end=10) xe,ye,ze,the,phe,enel,x0,y0,z0
    ...some mathematical calculations
   end do
10 close(1)
我现在需要做的是将我的计算结果添加到同一文件的末尾,并在我计算的行之后继续读取我的文件


在Fortran中如何实现这一点?

您可以通过在阅读过程中跟踪您所在的行来轻松实现这一点。但是,您需要确保您有一个紧急出口,因为当问题被问到时,循环将不会结束,直到您填满磁盘

我也怀疑这是否必要。我会使用一个可分配数组,将其设置为比您认为需要的大,然后有一个例程来检查计数并调整某些块的大小

在任何情况下,这里都有一个完整的功能示例:

program test
  implicit none
  integer :: iunit, max
  integer :: iline
  real :: xe,ye,ze,the,phe,enel,x0,y0,z0

  iunit = 1
  max = 20

  open(iunit,file='my_file.txt',status='old')

  iline = 0
  do
     iline = iline + 1
     read(iunit,*,end=10) xe, ye, ze, the, phe, enel, x0, y0, z0

     ! call calculation(?)
     xe = xe / 1000. ! just to see a difference in the file

     call append(iunit, iline, xe, ye, ze, the, phe, enel, x0, y0, z0)

     ! bettter have this emergency exit, because file will never hit end using append
     if (iline > max) exit
  end do
10 close(iunit)

contains
  subroutine append(iunit, iline, xe, ye, ze, the, phe, enel, x0, y0, z0)
    implicit none
    integer, intent(in) :: iunit, iline
    real, intent(in) :: xe, ye, ze, the, phe, enel, x0, y0, z0

    integer :: i

    ! skip to end
    do
       read(iunit,*,end=20)
    end do
20 continue
    backspace(iunit) ! back off the EOF

    ! append to file
    write(iunit,*) xe, ye, ze, the, phe, enel, x0, y0, z0

    ! rewind file and skip to iline
    rewind(iunit)
    i = 0
    do
       i = i + 1
       read(iunit,*)
       if (i == iline) exit
    end do

  end subroutine append

end program test

这是可能的,但你确定这就是你想要做的吗?我建议将其写入一个单独的文件,然后在完成所有操作后连接起来。这不仅会更简单,也省去了对干扰等问题的担心。但是,如果你确定…@francescalus我从物理的角度所做的,正在考虑二次电离:这些数字是电子的坐标和能量:这些电子将被传播,并与物质相互作用,产生其他电子,等等……因此产生的电子也将被传播。我把这些数字写在一个文件上,因为从一开始我就不知道会产生多少电子。@francescalus我的意思是,我看不到另一种方法可以做到这一点。另一种解决方法是将初始文件的内容和输出都写到另一个文件中。这样,您就不必对同一个文件进行读写操作,从而获得相同的效果。如果需要,您可以在以后丢弃原始文件。我理解这一点。我也做模拟,在大多数情况下,我只是在一个新文件中定期输出坐标。流程图(或类似信息)将帮助我们帮助您。更好的是,a.我已经尝试实现您的代码,但在执行时我得到了错误:
Fortran运行时错误:EOF标记后不允许顺序读取或写入,可能使用REWIND或BACKSPACE
I在Intel中测试这一点,这显然允许这种异常!您需要在append write语句之前添加语句“backspace(iunit)”。我编辑了我的答案。