Fortran 中的字符串和使用它们的文件名
这是麻烦...我正在动态构建(而不是更改)一个包含数字(数字)的字符串(例如文件名 out01.txt 、out02.txt 等..)
我的程序工作正常(我正在使用最后更新的值字符串来命名文件并编辑该文件)...但是在使用“ls”命令的同一目录中,我可以看到创建的文件,并且通过文件浏览器我可以访问它,但从命令行使用 vim , gedit i无法打开新文件同名正在打开...而且我无法从命令行删除该文件(rm out010.txt' 没有这样的文件或目录)这是代码,我可能无法解释我的问题,但代码会说明问题......
program strtest
implicit none
character(len=1024)::filen,format_str
integer::i
format_str="(a5,i0.3,'.txt')"
do i=1,10
write(filen,format_str)'out',i
end do
write(*,*)trim(filen)
open(23,file=trim(filen))
write(23,*)"what a mess!"
close(23)
stop
end program strtest
注意:即使在文件打开语句中没有使用trim()函数,我也有同样的问题,
请解释我的情况!!
问候 ...
here is the trouble ... i'm dynamically building (rather changing)a string which contains numerals(numbers) (like to have filename out01.txt ,out02.txt etc ..)
my program works fine (i'm using the last updated value string to name a file and edit that file) ... but in the same directory with "ls" command, i can see that file created and through file browser i can acess it but from command line using vim , gedit i can't open it new file of same name is opening... moreover i can't remove that file from command line (rm out010.txt'
no such file or directory) here is the code , i might not have been able to explain my problem but code will speak for itself ...
program strtest
implicit none
character(len=1024)::filen,format_str
integer::i
format_str="(a5,i0.3,'.txt')"
do i=1,10
write(filen,format_str)'out',i
end do
write(*,*)trim(filen)
open(23,file=trim(filen))
write(23,*)"what a mess!"
close(23)
stop
end program strtest
note: i have the same problem even without using trim() function in file opening statement
please explain my situation!!
regards ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如@jonsca 已经指出的那样,问题在于额外的空格。摆脱它的最简单方法是使用
adjustl
,如下所示:As @jonsca pointed out already, the problem is with the extra whitespace. The simplest way to get rid of it is to use
adjustl
, like this:您的文件名前面有 2 个空格,因此如果您输入
rm " out01.txt"
(2 个空格,out01.txt),您将能够删除它们。正是 a5 抛弃了格式字符串。Your filenames are coming out with 2 spaces in front of them, so if you put
rm " out01.txt"
(2 spaces,out01.txt) you will be able to delete them. It's the a5 that's throwing off the format string.