File 用任意随机名称打开文件的子例程
我提前表示歉意,因为已经有关于此类问题的帖子,但我是Fortran新手,我不理解它们。 我正在尝试用输入文件名生成一个子程序 我已经编写了以下代码,但它没有按预期工作File 用任意随机名称打开文件的子例程,file,fortran,File,Fortran,我提前表示歉意,因为已经有关于此类问题的帖子,但我是Fortran新手,我不理解它们。 我正在尝试用输入文件名生成一个子程序 我已经编写了以下代码,但它没有按预期工作 PROGRAM reading implicit none integer::dati,n character::namefile namefile=file.txt call read(n,dati,namefile) print*,'Number of data:',dati END PROGRAM reading
PROGRAM reading
implicit none
integer::dati,n
character::namefile
namefile=file.txt
call read(n,dati,namefile)
print*,'Number of data:',dati
END PROGRAM reading
SUBROUTINE read(n,num,namefile)
character::namefile
Integer::n
integer, intent(out)::num
open(40,file='namefile')
n=0
do
n=n+1
read(40,*,end=999)
enddo
999 continue
num=n-1
END SUBROUTINE read
谢谢原始代码对于使用
“
来划分名称感到困惑。有两个问题:
1) 在下一行中,文件名不包含在引号中(Fortran中可以使用”
,也可以使用“
,但两个文件对必须匹配),而是应该包含在引号中。因此更改
namefile=file.txt
到
2) 相反,在这方面
open(40,file='namefile')
变量名用引号括起来,不应使用引号。请将其更改为
open(40,file=namefile)
“namefile”指定文件名为namefile,而
namefile
指定文件名存储在名为namefile的变量中。原始代码对使用'
来分隔名称感到困惑。存在两个问题:
1) 在下一行中,文件名不包含在引号中(Fortran中可以使用”
,也可以使用“
,但两个文件对必须匹配),但应使用引号。所以改变
namefile=file.txt
到
2) 相反,在这方面
open(40,file='namefile')
变量名用引号括起来,不应为空。换成
open(40,file=namefile)
“namefile”指定文件名为namefile,而
namefile
指定文件名存储在名为namefile的变量中。有一个名为READ的内部变量,您的子例程名为READ,它也包含名为READ的内部变量
如果您的子例程使用了类似于子例程My_Reader的名称,那么这与内在读取不同
这应该是可行的,或者是接近的
PROGRAM reading
implicit none
integer :: dati, n
character(LEN=40) :: FileName
LOGICAL :: An_Error
FileName = 'file.txt'
call My_Reader(FileName, dati, An_Error)
IF(An_Error) THEN
WRITE(*,*)'I had an error finding file="',FileName(1:LEN_TRIM(FileName)),'"'
ELSE
print*,'Number of data:',dati
ENDIF
END PROGRAM reading
!=====================
SUBROUTINE My_Reader(FileName, Num, An_Error)
character, LEN=*, INTENT(IN ) :: FileName
integer , INTENT( OUT) :: num
LOGICAL , INTENT( OUT) :: An_Error
character(LEN=256) :: TextLine
Integer :: My_LUN
LOGICAL :: It_Exists
INQUIRE(File=FileName, EXIST=It_Exists)
IF(It_Exists) THEN
An_Error = .FALSE.
ELSE
An_Error = .TRUE.
RETURN
ENDIF
OPEN(NEWUNIT=My_LUN, FILE=FileName)
num = 0
DO WHILE (.TRUE.)
read(My_LUN,900,end=999) TextLine
900 FORMAT(A)
num = num + 1
enddo
999 continue
CLOSE(My_LUN)
REURN
END SUBROUTINE My_Reader
有一个名为READ的内在函数,您的子例程名为READ,它也包含名为READ的内在函数 如果您的子例程使用了类似于子例程My_Reader的名称,那么这与内在读取不同 这应该是可行的,或者是接近的
PROGRAM reading
implicit none
integer :: dati, n
character(LEN=40) :: FileName
LOGICAL :: An_Error
FileName = 'file.txt'
call My_Reader(FileName, dati, An_Error)
IF(An_Error) THEN
WRITE(*,*)'I had an error finding file="',FileName(1:LEN_TRIM(FileName)),'"'
ELSE
print*,'Number of data:',dati
ENDIF
END PROGRAM reading
!=====================
SUBROUTINE My_Reader(FileName, Num, An_Error)
character, LEN=*, INTENT(IN ) :: FileName
integer , INTENT( OUT) :: num
LOGICAL , INTENT( OUT) :: An_Error
character(LEN=256) :: TextLine
Integer :: My_LUN
LOGICAL :: It_Exists
INQUIRE(File=FileName, EXIST=It_Exists)
IF(It_Exists) THEN
An_Error = .FALSE.
ELSE
An_Error = .TRUE.
RETURN
ENDIF
OPEN(NEWUNIT=My_LUN, FILE=FileName)
num = 0
DO WHILE (.TRUE.)
read(My_LUN,900,end=999) TextLine
900 FORMAT(A)
num = num + 1
enddo
999 continue
CLOSE(My_LUN)
REURN
END SUBROUTINE My_Reader
我知道有些东西是没有意义的。我知道有些东西是没有意义的。因为一个叫做
read
的子程序和一个read
语句之间没有混淆,所以我不理解你最初的评论。我想我今晚学到了一些东西。。。我倾向于避免使用相同的名称,但可能是因为它会让我感到困惑是的-我不认为调用子例程read
是个好主意。因为名为read
的子例程和read
语句之间没有混淆我不理解您最初的评论,那么,我想今晚我学到了一些东西。。。我倾向于避免使用相同的名称,但可能是因为它会让我感到困惑是的-我不认为调用子例程read
是个好主意。