I have a question about file/directory handling in Fortran. How should a file located in the user’s home directory be accessed from a Fortran program? If I define a file name as follows, the program always stores it into the same directory where the binary is located.
I tried the usual Unix convention '~/file.txt', but it does not seem to work (files can’t be created). Is there some better way than just hard-coding the entire file path?
Background: I’m developing a note keeping application called fnote that should have its data stored at a fixed location (home dir).
Hi @art-rasa ,
see the man bash section Tilde Expansion. The tilde is a kind of shortcut in bash for the HOME environment variable. So you can not use it in Fortran which considers it as a simple character.
This allows you to know what your current OS is and than chose the correct path to store your app specific data, which would probably be $HOME/.local/share/fnote on Unix systems and %APPDATA%\local\fnote on Windows (using APPDATA on Windows is easier than HOMEDIR because it already includes the drive letter which is absent in HOMEDIR and therefore has to be queried separately).