Code Comments
Programming Forum and web based access to our favorite programming groups.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();
}
Post Follow-up to this messages8ngsu3@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
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.