NO

Author Topic: how can i retrieve file name from file handle ?  (Read 5501 times)

junko

  • Guest
how can i retrieve file name from file handle ?
« on: July 23, 2011, 11:51:15 PM »
is this possible ?

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: how can i retrieve file name from file handle ?
« Reply #1 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.
---
Stefan

Proud member of the UltraDefrag Development Team

CommonTater

  • Guest
Re: how can i retrieve file name from file handle ?
« Reply #2 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?


junko

  • Guest
Re: how can i retrieve file name from file handle ?
« Reply #3 on: July 24, 2011, 10:47:47 PM »
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

  • Guest
Re: how can i retrieve file name from file handle ?
« Reply #4 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);
« Last Edit: July 25, 2011, 05:55:57 AM by CommonTater »

junko

  • Guest
Re: how can i retrieve file name from file handle ?
« Reply #5 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.

CommonTater

  • Guest
Re: how can i retrieve file name from file handle ?
« Reply #6 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...