如何命名输出文件,使其与变量相关(在fortran中)? [英] How to name output files, making it related to a variable (in fortran)?

查看:2052
本文介绍了如何命名输出文件,使其与变量相关(在fortran中)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EG。整数变量(m)将逐个取下列值:

1 2 3 ,....



对于m的每个值,一个数组

<获得p>(p> i)(i = 1,2,...,1000)



并将其写入输出文件中,其中



open() write()



您可以告诉我如何将这些输出文件命名为

file1.dat file2.dat file3.dat ,...



谢谢。

解决方案

所以,这里有一个建议:

  integer :: m 
integer :: fu
字符(len = 10):: file_id
字符(len = 50):: file_name

!将整数写入一个字符串:
write(file_id,'(i0)')m

!构建文件名:
file_name ='file'// trim(adjustl(file_id))//'.dat'

!打开文件名为
open(file = trim(file_name),unit = fu)



<请注意,你也可以用iX.Y格式的字符串获得前导零。


Eg. The integer variable (m) will take the following values one by one:

1, 2, 3, ....

For each value of m, an array

p(i) (i=1,2,..., 1000)

is obtained and written in a output file with

open() and write()

Could you tell me how to name these output files as

file1.dat, file2.dat, file3.dat, …

Thanks.

解决方案

So, here is a suggestion:

integer :: m
integer :: fu
character(len=10) :: file_id
character(len=50) :: file_name

! Write the integer into a string:
write(file_id, '(i0)') m

! Construct the filename:
file_name = 'file' // trim(adjustl(file_id)) // '.dat'

! Open the file with this name
open(file = trim(file_name), unit = fu)

Note, that you could also obtain leading zeroes with the iX.Y format string.

这篇关于如何命名输出文件,使其与变量相关(在fortran中)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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