如何在fortran中的特定行上编写

如何在fortran中的特定行上编写,fortran,fortran90,gfortran,Fortran,Fortran90,Gfortran,我想从文件夹中复制一个文件,并使用fortran在文件的特定行写入。我正在使用Windows,GNU fortran编译器。下面是示例文件和代码。 file1.txt 1 * 2 ** 3 *** 4 **** 5 ***** 6 ****** 7 ******* 8 ******** 9 ********* 10 ********** 以下是代码: 我定义了一些变量。只有当两个条件匹配(特定变量值和行号)时,我才希望在文件中写入新文本。我尝试使用system命令进行复制,但失败了。谁能告诉我

我想从文件夹中复制一个文件,并使用fortran在文件的特定行写入。我正在使用Windows,GNU fortran编译器。下面是示例文件和代码。
file1.txt

1 *
2 **
3 ***
4 ****
5 *****
6 ******
7 *******
8 ********
9 *********
10 **********
以下是代码: 我定义了一些变量。只有当两个条件匹配(特定变量值和行号)时,我才希望在文件中写入新文本。我尝试使用
system
命令进行复制,但失败了。谁能告诉我正确的使用方法吗?并且程序得到运行时错误
未格式化文本存在格式

program read

   integer :: a,b,c,d,e
   CHARACTER (LEN=200) :: str

   a=0
   b=1
   c=0
   d=1
   e=0

    !call system ("copy" // "D:\test1\file1.txt"," ", // "D:")
    !This command fails

   open (unit=10, file="file1.txt", access="direct", & 
form="unformatted",  action="readwrite", recl=100 )

   do i=1,10,1
    read (10,*) str 

    if(a==0 .AND. i==3) then
        write(10,100) 'This is ',i,' line'
    else if(b==0 .AND. i==4) then
        write(10,100) 'This is ',i,' line'
    else if(c==0 .AND. i==5) then
        write(10,100) 'This is ',i,' line'
    else if(d==0 .AND. i==6) then
        write(10,100) 'This is ',i,' line'
    else if(e==0 .AND. i==7) then
        write(10,100) 'This is ',i,' line'
    100 format (2a,i0,1X)
    end if
   end do

   close (unit=10)  
end program 

我可以在同一个文件中读写吗?请分享你的评论。我提到了一些问题,但无法帮助我。

您的系统调用有几处问题。首先,在
copy
和第一个参数之间需要一个空格。其次,您需要一个目标文件,而不仅仅是一个文件夹。此外,您应该只使用字符串连接符
/
,而不是逗号。例如,如果要复制到新文件名
file2.txt
,可以使用如下系统调用:

call system ("copy " // "D:\test1\file1.txt " // "D:\test1\file2.txt")
因为您使用的是文字字符串而不是变量,所以可以通过去掉串联器来简化它:

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

您的系统调用有几个问题。首先,在
copy
和第一个参数之间需要一个空格。其次,您需要一个目标文件,而不仅仅是一个文件夹。此外,您应该只使用字符串连接符
/
,而不是逗号。例如,如果要复制到新文件名
file2.txt
,可以使用如下系统调用:

call system ("copy " // "D:\test1\file1.txt " // "D:\test1\file2.txt")
因为您使用的是文字字符串而不是变量,所以可以通过去掉串联器来简化它:

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

您的系统调用有几个问题。首先,在
copy
和第一个参数之间需要一个空格。其次,您需要一个目标文件,而不仅仅是一个文件夹。此外,您应该只使用字符串连接符
/
,而不是逗号。例如,如果要复制到新文件名
file2.txt
,可以使用如下系统调用:

call system ("copy " // "D:\test1\file1.txt " // "D:\test1\file2.txt")
因为您使用的是文字字符串而不是变量,所以可以通过去掉串联器来简化它:

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

您的系统调用有几个问题。首先,在
copy
和第一个参数之间需要一个空格。其次,您需要一个目标文件,而不仅仅是一个文件夹。此外,您应该只使用字符串连接符
/
,而不是逗号。例如,如果要复制到新文件名
file2.txt
,可以使用如下系统调用:

call system ("copy " // "D:\test1\file1.txt " // "D:\test1\file2.txt")
因为您使用的是文字字符串而不是变量,所以可以通过去掉串联器来简化它:

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

为了进行说明,以下是如何使用文本文件直接访问:

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
结果文件:

12345678 三点一四四二 qwertyui
为了进行说明,以下是如何使用文本文件直接访问:

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
结果文件:

12345678 三点一四四二 qwertyui
为了进行说明,以下是如何使用文本文件直接访问:

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
结果文件:

12345678 三点一四四二 qwertyui
为了进行说明,以下是如何使用文本文件直接访问:

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
结果文件:

12345678 三点一四四二 qwertyui
首先将“未格式化”更改为“格式化”。未格式化表示二进制,格式化为文本(或多或少)。但请注意,直接访问文件要求所有行具有相同的长度。您是如何使用
系统的
它是如何失败的?可以“就地”更新文件,但可能会很棘手。通常更直接的方法是读取文件并将行写入新文件,在运行过程中替换要替换的行。后一种方法将避免直接访问中的错误在第一次操作文件时留下的混乱。你觉得幸运吗?@HighPerformanceMark你能告诉我一种逐行复制文件的方法吗?首先将“未格式化”更改为“格式化”。未格式化表示二进制,格式化为文本(或多或少)。但请注意,直接访问文件要求所有行具有相同的长度。您是如何使用
系统的
它是如何失败的?可以“就地”更新文件,但可能会很棘手。通常更直接的方法是读取文件并将行写入新文件,在运行过程中替换要替换的行。后一种方法将避免直接访问中的错误在第一次操作文件时留下的混乱。你觉得幸运吗?@HighPerformanceMark你能告诉我一种逐行复制文件的方法吗?首先将“未格式化”更改为“格式化”。未格式化表示二进制,格式化为文本(或多或少)。但请注意,直接访问文件要求所有行具有相同的长度。您是如何使用
系统的
它是如何失败的?可以“就地”更新文件,但可能会很棘手。通常更直接的方法是读取文件并将行写入新文件,在运行过程中替换要替换的行。后一种方法将避免直接访问中的错误在第一次操作文件时留下的混乱。你觉得幸运吗?@HighPerformanceMark你能告诉我一种逐行复制文件的方法吗?首先将“未格式化”更改为“格式化”。未格式化表示二进制,格式化为文本(或多或少)。但请注意,直接访问文件要求所有行具有相同的长度。您是如何使用
系统的
它是如何失败的?可以“就地”更新文件,但可能会很棘手。通常更直接的方法是读取文件并将行写入新文件,在运行过程中替换要替换的行。后一种方法将避免直接访问中的错误在第一次操作文件时留下的混乱。你觉得幸运吗?@HighPerformanceMark你能告诉我一种逐行复制文件的方法吗?我只是想澄清一下:这项工作所需的唯一修改