Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get filename from a filehandle

Status
Not open for further replies.

bhsg

Programmer
Nov 1, 2002
2
GB
Given a filehandle is there a way of getting the filename of the file that the handle points to. The program will be running in protected mode DOS as it will be linked in with a Clipper application.
 
There's no straightforward standard way to do this. First, check your particular implementation's definition of a FILE. They're allowed to put whatever they want in there. If for some reason it actually has a field for the filename, use that (I'm not sure if it's common for there to be such a field or not, but I've never seen it in any implementation I've looked at).

Otherwise, if you really need it to be passed around with the structure, you'd have to make a struct type called NamedFile or something to that effect, and put a FILE* and a char* in there. Write a wrapper for open that initializes both of these, and pass around NamedFile* instead of FILE*.

Note that the above method only knows the name used to open the file. In UNIX, a file could feasibly have several "names". This, by the way, is probably part of the reason the feature you're looking for doesn't exist in a standard way.

The only other thing I can think of is to simply have the filename inside the file as the first line. Seek to the beginning and read the first sequence of characters any time you want to know the name. This requires that you format your files accordingly, of course.
 
Thanks for that but I'm not getting a FILE structure to start with just the int handle. Looks like I'll have to do it another way then.
 
Try the fdopen function, which returns a FILE structure from a file handle (of course, the handle must be obtained from a call to open).

You can then find all kinds of information in the FILE structure.

HTH. [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top