首页 > 解决方案 > Fortran 在目录中找不到输入文件

问题描述

我用fortran(f90,使用gfortran)编写了一个程序,它从一个.txt文件(列出o整数)接收输入。

  PROGRAM prog_example

    implicit none

    INTEGER i,j
    CHARACTER file1*20
    CHARACTER*8, DIMENSION(10,3000) :: data
    
    302 FORMAT (a20)

    WRITE(*,*)'Type the name of the input file:'  
    CALL SYSTEM("pwd") !show the directory    
    READ(*,302) file1
    OPEN(20,file=file1,status='old')     

END

当我运行代码并键入文件夹中的文件名 (in.txt) 时,它返回一个错误,指出它找不到该文件。这段代码在我的旧 Windows 计算机上运行得很好,但在我的 Mac 上却不行。可能是什么问题呢?

所有文件都在 Usr/documents/prog_example 中,其中包含:

Sypome5.cbp Sypome5.depend  Sypome5.exe Sypome5.layout  
a.out       bin     in.txt      main        main.f95    obj

我收到以下错误消息:

 At line 101 of file main.f95 (unit = 20) Fortran runtime error: Cannot open file 'in.txt': No such file or directory
 
 Error termination. Backtrace:
 #0  0x104620b17
 #1  0x10462178f
 #2  0x104622113
 #3  0x1046e6f23
 #4  0x1046e7157
 #5  0x10456155b
 #6  0x104562337
 #7  0x10456314b

添加打印目录的命令后,我了解到它在我的根目录 (Users/pedro) 中运行,但它应该在 Users/pedro/documents/prog_example 中运行。如果我将输入文件粘贴到根目录中,它就可以正常工作,但是将这些文件放在那里会很麻烦。

标签: macosfortran

解决方案


推荐阅读