Home > Archive > Fortran > December 2004 > The scope of a file descriptor
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 |
The scope of a file descriptor
|
|
| s8ngsu3@yahoo.com 2004-12-28, 9:12 am |
| Is the scope of a file descriptor limited to the subroutine/function
where the file is opened, or can it be passed as an argument to another
subroutine? I.e., is it possible to open a file in the main part of a
program and write/read the file from some subroutine? I'm looking for a
Fortran equivalent of doing the following in C++:
void some_output (fstream& fout, int x)
{
fout << x;
}
int main ()
{
fstream afile ("somefile.txt", ios::out);
some_output (afile, 5);
afile.close();
}
| |
| Arjen Markus 2004-12-28, 9:12 am |
| s8ngsu3@yahoo.com wrote:
>
> Is the scope of a file descriptor limited to the subroutine/function
> where the file is opened, or can it be passed as an argument to another
> subroutine? I.e., is it possible to open a file in the main part of a
> program and write/read the file from some subroutine? I'm looking for a
> Fortran equivalent of doing the following in C++:
>
> void some_output (fstream& fout, int x)
> {
> fout << x;
> }
>
> int main ()
> {
> fstream afile ("somefile.txt", ios::out);
> some_output (afile, 5);
> afile.close();
> }
Fortran does not use file descriptors: files in Fortran programs
are opened to a particular logical unit number, simply an integer
value. This is _global_ to the whole program and as long as you
do not close the file (using that same integer value) or open
another file using that same value, the connection remains.
Safe values for the LU number are (generally speaking) between
10 and 99.
The above could read:
subroutine some_output( luout, x )
integer :: luout, x
write(luout,*) x
end subroutine
program myprogram
integer :: luout
luout = 10
open( luout, 'somefile.txt' )
call some_output( luout, 5 )
close( luout )
end program
You do not need to use a variable, but it helps keeping
the code clean :)
You can use the INQUIRE statement to find out what if any
file a LU number is connected to and other properties.
Regards,
Arjen
|
|
|
|
|