how can i retrieve file name from file handle ?

Started by junko, July 23, 2011, 11:51:15 PM

Previous topic - Next topic

Stefan Pendl

GetFileInformationByHandleEx can be used to get this information, but there are requirements for Windows versions below Vista.
---
Stefan

Proud member of the UltraDefrag Development Team

CommonTater

I know this is a dumb question... but why wouldn't you save the filename you used to open it?


junko

Quote from: Stefan Pendl on July 24, 2011, 12:39:42 AM
GetFileInformationByHandleEx can be used to get this information, but there are requirements for Windows versions below Vista.
thank you very much
i'm sorry i forgot to mention, i'm talking about FILE stream handle
(i know how to convert to windows api hanlde)
because i want to use crossplatforms functions

CommonTater

#4
Once again... you had to have the filename to open the file, whether it is by fopen() or CreateFile().

If the filename is user input, you already have it in a string, just save the string.
If the filename is "hard coded" make it a #define and use that.

The point being that in order to open the file you had to know it's name at some point... why would you not just use that?


#define FILENAME "c:\\myspace\\myfile.txt"    // global filename

// later
FILE *fp = fopen(FILENAME,"r+");

// still later
printf("The filename is : %s,FILENAME);


or

char FileName[256] = {0};    // declared at global scope

// later
fprint("Enter the filename : ");
fgets(FileName,255,stdin);
fopen(FileName,"r+");

// still later
printf(The filename is : %s",FileName);


junko

i think i know how to save the file name when i open it, thanks :)
it's not relevant here because the file is opened by a user,
and i don't want to save the user input
so i'm looking for a way to retrieve this from the file handle,
which i'm sure it's possible,
since the stream probably knows its destination :)
the answer of stephen is good, i'm just curious maybe there is
an os independent function to do this.

CommonTater

Quote from: junko on July 25, 2011, 10:53:34 PM
i think i know how to save the file name when i open it, thanks :)
it's not relevant here because the file is opened by a user,
and i don't want to save the user input
so i'm looking for a way to retrieve this from the file handle,
which i'm sure it's possible,
since the stream probably knows its destination :)
the answer of stephen is good, i'm just curious maybe there is
an os independent function to do this.

Ok, well, good luck with that...