# Accessing a file in user's home directory

**URL:** <https://fortran-lang.discourse.group/t/accessing-a-file-in-users-home-directory/758>\
**Category:** Help\
**Created:** [February 26, 2021, 8:11pm UTC](https://fortran-lang.discourse.group/t/accessing-a-file-in-users-home-directory/758 "2021-02-26T20:11:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![art-rasa](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@art-rasa](https://fortran-lang.discourse.group/u/art-rasa)\
**Post date:** [February 26, 2021, 8:11pm UTC](https://fortran-lang.discourse.group/t/accessing-a-file-in-users-home-directory/758/1 "2021-02-26T20:11:06Z")

</div>

Hello all,

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.

`character(len=*), parameter :: FILENAME = 'file.txt'`

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](https://github.com/art-rasa/fnote) that should have its data stored at a fixed location (home dir).

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [February 26, 2021, 8:25pm UTC](https://fortran-lang.discourse.group/t/accessing-a-file-in-users-home-directory/758/2 "2021-02-26T20:25:18Z")

</div>

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.

You should read your environment variable HOME from your program:  
[https://gcc.gnu.org/onlinedocs/gfortran/GET\_005fENVIRONMENT\_005fVARIABLE.html](https://gcc.gnu.org/onlinedocs/gfortran/GET_005fENVIRONMENT_005fVARIABLE.html)  
You have the example at the end of the page.

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [February 26, 2021, 8:36pm UTC](https://fortran-lang.discourse.group/t/accessing-a-file-in-users-home-directory/758/3 "2021-02-26T20:36:15Z")

</div>

> [@art-rasa](#):
>
> Hello all,
> 
> 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.
> 
> `character(len=*), parameter :: FILENAME = 'file.txt'`

The OPEN statement will look for `file.txt` in the current working directory, which may differ from the directory of the binary.

---

<div class="post-metadata">

**Author:** ![art-rasa](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@art-rasa](https://fortran-lang.discourse.group/u/art-rasa)\
**Post date:** [February 26, 2021, 10:06pm UTC](https://fortran-lang.discourse.group/t/accessing-a-file-in-users-home-directory/758/4 "2021-02-26T22:06:04Z")

</div>

@vmagnin  
Thanks. It is exactly the information I was looking for.

@Beliavsky  
Indeed, that’s a good point. When I run the program with “`fpm run`” it creates the file in the current (project root) directory.

---

<div class="post-metadata">

**Author:** ![awvwgk](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/awvwgk/32/154_2.png) [@awvwgk](https://fortran-lang.discourse.group/u/awvwgk)\
**Post date:** [February 27, 2021, 1:35pm UTC](https://fortran-lang.discourse.group/t/accessing-a-file-in-users-home-directory/758/5 "2021-02-27T13:35:57Z")

</div>

You might be interested in using `stdlib_os` here:

> **[MarDiehl/stdlib\_os](https://github.com/mardiehl/stdlib_os)**
>
> Contribute to MarDiehl/stdlib\_os development by creating an account on GitHub.

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).

---

<div class="post-metadata">

**Author:** ![drikosev](https://avatars.discourse-cdn.com/v4/letter/d/e47c2d/32.png) [@drikosev](https://fortran-lang.discourse.group/u/drikosev)\
**Post date:** [February 28, 2021, 1:49am UTC](https://fortran-lang.discourse.group/t/accessing-a-file-in-users-home-directory/758/6 "2021-02-28T01:49:04Z")

</div>

Hello,

A recent discussion in c.l.f raised a similar issue. The thread is:  
[https://groups.google.com/g/comp.lang.fortran/c/KYnTjAYYJ6k](https://groups.google.com/g/comp.lang.fortran/c/KYnTjAYYJ6k)  
How to tell what operating system a Fortran executable is running on?

Then, I wrote a Fortran program that locates the user home directory in  
various environments, ie Linux, macOS, Windows, and so on. The  
program itself is dummy. You simply copy paste the routines you want,  
see [Hardcode or find at Runtime OS Name and File Separator in Fortran (by m\_sys.java). · GitHub](https://gist.github.com/drikosev/c5b650e8cf2e0ec68d9ba193c6ace47f)

Regards,  
Ev. Drikos
