In my class I have FILE* ascii_file;
data member, which always is initialized via class constructor. How can I get full path of the asci_file
? I don't want to store more information about the file expect ascii_file
, and want it to work on windows, linux and solaris.
Asked
Active
Viewed 9,574 times
5

Mihran Hovsepyan
- 10,810
- 14
- 61
- 111
-
Any reason that you can't add a filename param to the ctor? – Ivo Wetzel Nov 29 '10 at 15:55
-
2possible duplicate of [Getting Filename from file descriptor in C](http://stackoverflow.com/questions/1188757/getting-filename-from-file-descriptor-in-c) – Svisstack Nov 29 '10 at 15:59
-
2Same as this question? http://stackoverflow.com/questions/831004/determining-the-opened-filename-from-a-file – Matt K Nov 29 '10 at 16:00
-
1@Svisstack/@Matt Kane - this is not a dup, each q has only Linux-specific responses – Steve Townsend Nov 29 '10 at 16:01
2 Answers
4
Here is a solution for Linux:
4
This task is going to require non-portable code.
On Windows you can convert FILE*
to CRT file descriptor using _fileno, and then convert to an OS handle using _get_osfhandle.
Then you can get the filename as shown here (using file mapping).
Obtaining a File Name From a File Handle

Steve Townsend
- 53,498
- 9
- 91
- 140