将整数转换为字符串以在运行时创建输出文件名 [英] Convert integers to strings to create output filenames at run time

查看:183
本文介绍了将整数转换为字符串以在运行时创建输出文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Fortran中有一个程序将结果保存到一个文件中。目前我使用

I have a program in Fortran that saves the results to a file. At the moment I open the file using

OPEN (1, FILE = 'Output.TXT')

但是,我现在要运行一个循环,并将每次迭代的结果保存到 Output1.TXT''Output2.TXT''Output3.TXT',等等。

However, I now want to run a loop, and save the results of each iteration to the files 'Output1.TXT', 'Output2.TXT', 'Output3.TXT', and so on.

在Fortran中有一种简单的方法来构建来自循环计数器 i ?的文件名。你可以写入一个单元,但你也可以写入一个字符串

Is there an easy way in Fortran to constuct filenames from the loop counter i?

推荐答案

program foo
    character(len=1024) :: filename

    write (filename, "(A5,I2)") "hello", 10

    print *, trim(filename)
end program



<请注意(这是我所说的第二个技巧),您还可以通过编程方式构建一个格式化字符串。

Please note (this is the second trick I was talking about) that you can also build a format string programmatically.

program foo

    character(len=1024) :: filename
    character(len=1024) :: format_string
    integer :: i

    do i=1, 10
        if (i < 10) then
            format_string = "(A5,I1)"
        else
            format_string = "(A5,I2)"
        endif

        write (filename,format_string) "hello", i
        print *, trim(filename)
    enddo

end program

这篇关于将整数转换为字符串以在运行时创建输出文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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