将整数转换为字符串以在运行时创建输出文件名
我有一个 Fortran 程序,可以将结果保存到文件中。 目前我使用打开文件
OPEN (1, FILE = 'Output.TXT')
但是,我现在想要运行一个循环,并将每次迭代的结果保存到文件 'Output1.TXT'
, 'Output2.TXT'< /code>、
'Output3.TXT'
等。
Fortran 中有没有一种简单的方法可以从循环计数器 i 构建文件名?
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')
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.
Is there an easy way in Fortran to constuct filenames from the loop counter i
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您可以写入一个单元,但也可以写入一个字符串
请注意(这是我谈论的第二个技巧),您还可以以编程方式构建格式字符串。
you can write to a unit, but you can also write to a string
Please note (this is the second trick I was talking about) that you can also build a format string programmatically.
恕我直言,一个更简单的解决方案............
A much easier solution IMHO ...................
这是一个简单的函数,它将返回整数的左对齐字符串版本:
这是一个测试代码:
Well here is a simple function which will return the left justified string version of an integer:
And here is a test code:
我已经在 SO 的其他地方展示了这一点(如何在格式说明符语句中使用变量?,不是完全重复的恕我直言) ,但我认为把它放在这里是值得的。 可以使用此问题的其他答案中的技术来创建一个简单的函数
,您可以在之后使用该函数,而无需担心修剪和左调整,也无需写入临时变量:
由于可分配字符串,它需要 Fortran 2003。
I already showed this elsewhere on SO (How to use a variable in the format specifier statement? , not an exact duplicate IMHO), but I think it is worthwhile to place it here. It is possible to use the techniques from other answers for this question to make a simple function
which you can use after without worrying about trimming and left-adjusting and without writing to a temporary variable:
It requires Fortran 2003 because of the allocatable string.
对于缩短版本。
如果所有索引都小于 10,则使用以下内容:
对于通用版本:
仅此而已。
For a shorten version.
If all the indices are smaller than 10, then use the following:
For a general version:
That's all.
我已经尝试过 @Alejandro 和 @user2361779 但它给了我一个不满意的结果,例如
file 1.txt
或file1 .txt
而不是file1.txt< /代码>。 不过我找到了更好的解决方案:
解释:
例如 set
i = 10
和write(char_i, '(I5)') i
我认为这是一个最简单的解决方案,可以为您提供从整数到字符串转换过程中没有任何遗留空格的动态长度文件名。
I've tried @Alejandro and @user2361779 already but it gives me an unsatisfied result such as
file 1.txt
orfile1 .txt
instead offile1.txt
. However i find the better solution:Explanation:
e.g. set
i = 10
andwrite(char_i, '(I5)') i
I think this is a simplest solution that give you a dynamical length filename without any legacy blank spaces from integer to string conversion process.
请尝试以下操作:
其中“...”表示适合您目的的其他适当代码。
Try the following:
Where "..." means other appropriate code for your purpose.
这是在循环中为不同参数写入文件的答案
Here is the answer for writing files for different parameters in a loop
要将整数转换为字符串:
To convert an integer to a string:
这是我解决这个问题的子程序方法。 它将 0 : 9999 范围内的整数转换为字符。 例如,INTEGER 123 转换为字符 0123。希望对您有所帮助。
PS - 抱歉评论; 它们在罗马尼亚语中有意义:P
Here is my subroutine approach to this problem. it transforms an integer in the range 0 : 9999 as a character. For example, the INTEGER 123 is transformed into the character 0123. hope it helps.
P.S. - sorry for the comments; they make sense in Romanian :P