问题
The folder containing my main executable is very cluttered with input, output and source files. I would like to move some of these files into a different directory to my executable but still be able to access them. It would be fantastic if I could make a directory, for example ./main/outfile
, that holds all the output files from my program. Is it possible to include a path when accessing these files in Fortran 77/90?
If compilers are of any importance, I use gfortran which is running on Ubuntu 11.10.
回答1:
for input/output files, just specify the path when you are opening the file. For example:
open(unit=3,file='outputdata/data_modified.txt',status='unknown')
will open a file in the outputdata
folder. Note that the outputdata
folder has to exist beforehand, or you will likely get an error.
回答2:
The starting directory at runtime is not the directory where your main program happens to reside, but rather the current directory when starting the program.
For instance
mkdir run_N && cd run_N && ../my_program
Will read and write files in the current directory (./run_N) even though the application binary is in another directory.
来源:https://stackoverflow.com/questions/9573475/accessing-files-in-sub-directory-of-main-program