Fortran;遍历具有共同属性的文件名 [英] Fortran; looping over file names with common attributes

查看:155
本文介绍了Fortran;遍历具有共同属性的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Fortran还是很陌生,我的文件名遇到了麻烦,我在simuln#.res(其中1<#< 20)中有一堆数据,我有多个相同的多个目录simuln#.res名称,但是它们具有不同的输入参数.代码如下:

I'm fairly new to Fortran and I am having trouble with my file names, I have a bunch of data in simuln#.res (where 1<#<20), I have multiple different directories with all the same simuln#.res names but they had different input parameters. The code looks like this:

  character(len=11) :: theFileA
  character(len=12) :: theFileB
  character(len=:), allocatable :: fileplace

  write(*,*) "the directory with the data sets, use quotations"
  read(*,*) fileplace

  fileLoop : do j=1,20
  if (j .lt. 10) then 
    write(theFileA, '("simuln", I1,".res")' ) j   
    open(newunit= iin,file = fileplace//theFileA,status='old')
 else 
    write(theFileB, '("simuln",I2,".res")') j
    open(newunit= iin,file = fileplace//theFileB,status='old')
 end if

does some stuff with the file 
end do fileLoop

代码在我的Mac上使用gfortran编译器进行编译,但是当我将路径放入包含文件的目录时,它会给出错误simuln1.res does not exist(这确实是错误的,请三重检查).我试过更改编辑描述符(并使real(j)),但是我仍然得到相同的结果. 谁能帮我吗?

The code compiles with a gfortran compiler on my mac, but when I put in my path to the directory with the files, it gives the error simuln1.res does not exist (which it absolutely does, triple checked). I have tried changing the edit descriptor (and making real(j)), but I still get the same thing. Can anyone help me?

推荐答案

您有fileplace的延迟长度((len=:)),但是在尝试读取之前似乎没有分配它.

You have fileplace of deferred length ((len=:)), but you appear to not allocate it before attempting the read.

也就是说,read(*,*) fileplace不会根据F2003自动分配规则将fileplace分配给正确的长度并进行分配.这意味着以后在fileplace上很可能会在要打开的文件中将其视为零长度字符变量('').

That is, read(*,*) fileplace doesn't, under the F2003 rules of automatic allocation, allocate fileplace to the correct length and assign. That means that later on fileplace could well be being treated as a zero-length character variable ('') in the file to be opened.

要检查此假设,请尝试print *, fileplace//theFileA.错误消息仅指向文件名的结尾部分,这一事实可以证明这一点.

To check this hypothesis, try print *, fileplace//theFileA. This could be supported by the fact that the error message refers to just the trailing part of the file's name.

在这种情况下,请使用大"变量.您说90个字符就可以了,所以:

If this is the case, then use a "large" variable. You say 90 characters is as long as you need, so:

character(len=90) :: fileplace    ! Adjust length as desired
...
read(*,*) fileplace
...
open (newunit=iin, file=TRIM(fileplace)//theFileA, status='old')
...

确保将文件名附加到修剪后的目录名中,以避免两部分之间留有空格.

Ensure you append the file's name to the trimmed directory name to avoid having spaces between the two parts.

[作为补充,您似乎不需要theFileAtheFileB;考虑到忽略尾随空格,只需使用后者即可.并且您可能希望在fileplace上强制使用尾随'/'.]

[As a side note, you appear to not need theFileA and theFileB; just use the latter, considering that trailing blanks are ignored. And you may well want to force a trailing '/' on fileplace.]

这篇关于Fortran;遍历具有共同属性的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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