Go Premium for a chance to win a PS4. Enter to Win


Filename from FILE struct ?

Posted on 2000-05-03
Medium Priority
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

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.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


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 200 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

824 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