Filename from FILE struct ?

Can I get the filename from a FILE structure or file handle using VC++ 6.0 ? How ?
Who is Participating?
saurabh_dasguptaConnect With a Mentor Commented:
My observations are as follows:

1)The structure FILE  is OS specific.  
Let us assume that we are on Win NT

2)In that case, the FILE structure has a member variable: int _file

3)Your file operations like fopen, fwrite, fprintf utlimately wrapup  corresponding Win32 API calls.

Therefore ,
fopen --> leads to CreateFile
fwrite,fprintf--> lead to WriteFile
fread, fscanf lead --> lead to ReadFile

Looking at the implementation of the C runtime functin calls I can say that ,
the _file member in the structure FILE is an index into an array where the actual file HANDLE is stored.
This array is defined in the "OsInfo.c", and is available for inspection in the CRT directory of MSVC product.
As and when you open more files this array keeps growing in size.

The array is named as _osfhnd[], so the following equation would hold true:
FILE *fp = fopen(".." , "r");
HANDLE hW32 = _osfhnd[fp->_file];
ReadFile(HW32, ...); or fread(fp, ..)

Assuming that you can make some use of  the code in the CRT folder, you would still need  a published API that gives you the name of the file , when you are providing  the file HANDLE.

I bet there would be some undocumented internal API for this purpose.

So , my answer in a nutshell is :
There is no simple and a direct way of getting the file name from a FILE structure.

You will have to resort to some sort of mapping ,that will be specific to you.
what do you mean by file structure???
or drived from it???
WalluceAuthor Commented:
The "FILE" structure, as used in stdout.h e.g. :

FILE *f;
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

I think this is os specific. No ANSI solution.
WalluceAuthor Commented:
Dos, windows and unix and linux has stdout.h .It comes standard with VC, borland C, gcc, cc and as far as I know with djgpp. In any case, I did specify that I am working with MSVC++ 6. I am not really looking for an ANSI solution. I need one that will work with MSVC++ 6.
The "FILE" structure is used with function like fopen, fread, fwrite, etc.
There is no standard, portable way to do this.  The C standard does not define what the The "FILE *" pointer used by the C stream procedures actually points. to.  In many cases it may not actually point to a structure.  It may simply be a an OS-dependant file descriptor or file handle.

if you need this sort of feature, you may want to create your own file class that stores this sort of information

Also you may wisth to switch to the C++ file stream objects (fstream).  They are strongly recommended over the C file streams (FILE *).
WalluceAuthor Commented:
Thank you Nietod. Do you know of a non-standard, non-portable way of doing this ? I just want to test my app in MSVC++ under Windows. I want to do this for debug purposes only.
MSVC doesn't really provide a way to do this  The FILE * pointer is a pointer to a structure, but they do not usually record the file name in the structure (there is room for it, but I think it is only used for temporary files, not for ordinary use.)  You can get the actual windows file handle, but that doesn't help you since the OS doesn't provide a mechansim for getting the name from the handle

Your best bet might be to create an fopen() interface function that calls the real fopen() function and then places the returned FILE *and the file name in a map<>.  Then you can search that map for the FILE* and get back the file name

I'm going on vacation, so I probalby can't respond to further requests.
Hi, Can you be more specific about what you want to do ? maybe we can help you without making you change to fstream.

What I didn't understand is why do you whant to do it ? i.e. since you are the one who is opening the file why can't you save the file name somewhere ?
WalluceAuthor Commented:
Someone else wrote the code. I was trying to debug through his code. At runtime, I wanted to run a function in the quickwatch window to check what file he was writing to so that I can see what's in that file. I know this might sound stupid, but I don't know what the filename is, because it is made up of variables , e.g. :
sprintf(szfilename, "%8.8s%d%s", username, userno, password);

The variables are local, so they are destroyed. I have managed to figure out the right filename, though. Thanx for the help.
just a quick comment: i don't know about you guys, but i think it's not very safe to use the password as part of a filename! perhaps you should consider changing that...

my 0.02$
WalluceAuthor Commented:
I just want to mention that the username password thing was just an example.

Thank you guys for the help;
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.