Home > Archive > Fortran > January 2008 > How to write an output file into a specific directory in Linux?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How to write an output file into a specific directory in Linux?
|
|
| hermitian 2008-01-24, 7:24 pm |
| 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
| |
|
| On 24 jan, 15:57, hermitian <iamwu...@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.
| |
|
| 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
|
|
|
|
|