Re: Filestream structs in C

Roland John Turner (rjturner@nospam.socs.uts.edu.au)
Sun, 29 Oct 1995 11:00:33 +1100 (EST)

Deadman wrote:
>
> Can anyone tell me if the struct behind the FILE* used by fopen and
> associated functions contains the filename used to open the file, and if
> so what the member is called?
> i.e. the problem is that i have to declare a struct including a FILE* and
> a filename string in order for error handling routines to output the name
> of the file on which errors occured.

It's not a meaningful question. (I assume that you are talking about a
UNIX environment.) A (FILE *) is attached to a specific OS file handle.
These handles are small integers. In a UNIX environment this handle is
available as (FILE *)thing->_file. This file handle does NOT however
have a filename associated with it. It can have come from a variety of
sources:

- A socket. Most sockets (eg Internet domain sockets) do not have filenames.
Look at what happens when you perform an get during an FTP session. The
remote server reads a local (named) file, but writes a file descriptor
which is merely one end of a TCP stream. It can have a (FILE *) structure
associated with it (see fdopen()), but no filename.

- An ordinary file. Unfortunately and ordinary file can have any number
of names in the filesystem, from 0 upwards. The obvious case is that
where you have hard linked files where several names, possibly in
different directories, refer to the same inde and thus the same file.
All the OS keeps is an inode number (well, vnode, but the problem is
the same). Worse than this, the last name referring to a file can
in fact be deleted (in UNIX, deleting a file is actually achieved
with unlink()). If someone still has the file open, its blocks
won't be freed, so the user will believe that they have an
ordinary file, yet it will have no name...

What are you actually trying to achieve?

- Raz #

rjturner@nospam.socs.uts.edu.au

"It often upsets a man's God fantasies to have (Misquoted? from )
someone shoot down one of his helicopters." (Ben Elton's "Stark" )