Pelles C forum

C language => Beginner questions => Topic started by: junko on July 23, 2011, 11:51:15 PM

Title: how can i retrieve file name from file handle ?
Post by: junko on July 23, 2011, 11:51:15 PM
is this possible ?
Title: Re: how can i retrieve file name from file handle ?
Post by: Stefan Pendl on July 24, 2011, 12:39:42 AM
GetFileInformationByHandleEx (http://msdn.microsoft.com/en-us/library/aa364953%28VS.85%29.aspx) can be used to get this information, but there are requirements for Windows versions below Vista.
Title: Re: how can i retrieve file name from file handle ?
Post by: CommonTater on July 24, 2011, 01:46:32 AM
I know this is a dumb question... but why wouldn't you save the filename you used to open it?

Title: Re: how can i retrieve file name from file handle ?
Post by: junko on July 24, 2011, 10:47:47 PM
GetFileInformationByHandleEx (http://msdn.microsoft.com/en-us/library/aa364953%28VS.85%29.aspx) 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
Title: Re: how can i retrieve file name from file handle ?
Post by: CommonTater on July 25, 2011, 05:54:18 AM
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?

Code: [Select]
#define FILENAME "c:\\myspace\\myfile.txt"    // global filename

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

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

or
Code: [Select]
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);
Title: Re: how can i retrieve file name from file handle ?
Post by: 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.
Title: Re: how can i retrieve file name from file handle ?
Post by: CommonTater on July 25, 2011, 11:31:19 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...