如何在fortran中的特定行写入 [英] How to write at specific lines in fortran

查看:25
本文介绍了如何在fortran中的特定行写入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to copy a file from a folder and write at specific lines of the file using fortran. I am using Windows, GNU fortran compiler. Here is sample file and code. file1.txt

1 *
2 **
3 ***
4 ****
5 *****
6 ******
7 *******
8 ********
9 *********
10 **********

代码如下:我定义了一些变量.只有当两个条件匹配(特定变量值和行号)时,我才想在文件中写入新文本.我尝试使用 system 命令进行复制,但它失败了.谁能告诉我正确的使用方法?并且程序出现运行时错误FORMAT present for unformatted text

Here is code: I defined some variables. Only if two criteria match (particular variable value and line number), I want to write in the new text in the file. I tried using system command to copy, but it fails. Can anyone tell me correct way of using this? And program got run time error FORMAT present for unformatted text

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:	est1file1.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 

我可以在同一个文件中读写吗?请分享您的意见.我提到了一些问题 Fortran - 如何编写数据到文件中的特定行?但无法帮助我.

Can I read and write in the same file? Please share your comments. I referred to some questions Fortran - How to write data to specific line in the file? but couldn't help me.

推荐答案

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

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:	est1file1.txt " // "D:	est1file2.txt")

因为你使用的是文字字符串而不是变量,你可以通过去掉连接符来简化它:

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

call system ("copy D:	est1file1.txt D:	est1file2.txt")

这篇关于如何在fortran中的特定行写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆