How to write at specific lines in fortran

人走茶凉 提交于 2019-11-28 14:44:10

There are a couple things wrong with your system call. First, you need a space between copy and the first argument. Second, you need a destination file, not just a folder. Also, you should only be using string concatenaters //, not commas. For example, if you want to copy to a new file name file2.txt, you can use a system call like this:

call system ("copy " // "D:\test1\file1.txt " // "D:\test1\file2.txt")

Because you're using literal strings instead of variables, you can simplify it by getting rid of the concatenaters:

call system ("copy D:\test1\file1.txt D:\test1\file2.txt")

for illustration, here is how to direct access work with a text file:

implicit none
character*8 x
! create a test file, all lines 8 characters:
open(20,file='test.txt')
x='12345678'
write(20,'(a)')x
x='asdfghjk'
write(20,'(a)')x
x='qwertyui'
write(20,'(a)')x
close(20)
! open file direct access, note record length is 8+2 because I'm
! stuck on DOS today with cr/lf line ends
open(20,file='test.txt',access='direct',recl=10,form='formatted')
! read whatever we want
read(20,'(a)',rec=3)x
write(*,*)'line 3 is',x
! overwrite a particular line -- note the format is exactly 10 char
! including the manually added line ending
write(20,'(f5.2,i3,2a)',rec=2)3.14,42,char(13),char(10)
end

resulting file:

12345678
 3.14 42
qwertyui
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!