From: hermitian on
Hi, everyone:

I can write an output file in the current working directory in Linux.
For example:

program ex
implicit none
open(unit=10,file="ex.txt")
write(10,*) "Hello!"
close(10)
stop
end

But how can I write the file "ex.txt" directly to a specific directory
in Linux or other *nix environment, i.e. $HOME or /tmp et al.

Moreover, is there any self-teaching resources on Fortran programing
in *nix environment?
For example, how to handle bash and other unix utilities with Fortran
and some special I/O syntax in *nix.

Thanks
From: fj on
On 24 jan, 15:57, hermitian <iamwu...(a)gmail.com> wrote:
> Hi, everyone:
>
> I can write an output file in the current working directory in Linux.
> For example:
>
> program ex
> implicit none
> open(unit=10,file="ex.txt")
> write(10,*) "Hello!"
> close(10)
> stop
> end
>
> But how can I write the file "ex.txt" directly to a specific directory
> in Linux or other *nix environment, i.e. $HOME or /tmp et al.
>
> Moreover, is there any self-teaching resources on Fortran programing
> in *nix environment?
> For example, how to handle bash and other unix utilities with Fortran
> and some special I/O syntax in *nix.
>
> Thanks

Easily :

open(unit=10,file="/tmp/ex.txt")

If you want to use environment variables like $HOME, this is a little
bit more difficult. You need to get the value of the environment
variable first :

CHARACTER(255) :: home
CALL get_environment_variable('HOME',home)
OPEN(unit=10,file=TRIM(home)//"/ex.txt")
....

If the routine get_environment_variable (F2003) is not available, you
can try getenv instead.

From: Hani on
hermitian wrote:

> Hi, everyone:
>
> I can write an output file in the current working directory in Linux.
> For example:
>
> program ex
> implicit none
> open(unit=10,file="ex.txt")
> write(10,*) "Hello!"
> close(10)
> stop
> end
>
> But how can I write the file "ex.txt" directly to a specific directory
> in Linux or other *nix environment, i.e. $HOME or /tmp et al.

Command-line redirection could be an alternative.

Write to Stdout (standard output: unit=*) and use
command-line-redirection of the bash shell, e.g.:

$ ./ex >/home/id/ex.txt

">" writes the output which is generally printed to the console (Stdout)
to the file ex.txt in /home/id. With ">>" insted of ">" you
append the output to an exiting file. With "<" you can read from Stdin
(standard input),e g.

$ prog <inputfile >outputfile

This works with all programs which write/read from/to Stdin/Stdout. I
like this way to do input/output because I do not need to implement
real file in-/output routines in my programs. The OS do it
for you and there are so many other advantages, like piping, redirection
to printers or other devices, ...

.... and command-line redirection works in Windows, too.

> Moreover, is there any self-teaching resources on Fortran programing
> in *nix environment?
> For example, how to handle bash and other unix utilities with Fortran
> and some special I/O syntax in *nix.

You need a bash tutorial. I can not recommend a web
address at the moment. Maybe others can do.

Hani