Filename from FILE struct ?

Posted on 2000-05-03
Last Modified: 2008-02-01
Can I get the filename from a FILE structure or file handle using VC++ 6.0 ? How ?
Question by:Walluce
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 2772583
what do you mean by file structure???
or drived from it???

Author Comment

ID: 2772784
The "FILE" structure, as used in stdout.h e.g. :

FILE *f;

Expert Comment

ID: 2772808
I think this is os specific. No ANSI solution.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 2772826
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.
LVL 22

Expert Comment

ID: 2772829
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 *).

Author Comment

ID: 2772837
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.
LVL 22

Expert Comment

ID: 2773064
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.

Expert Comment

ID: 2773512
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 ?

Author Comment

ID: 2773896
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.

Expert Comment

ID: 2774610
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$

Accepted Solution

saurabh_dasgupta earned 50 total points
ID: 2774814
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.

Author Comment

ID: 2783581
I just want to mention that the username password thing was just an example.

Thank you guys for the help;

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question