Solved

Filename from FILE struct ?

Posted on 2000-05-03
12
208 Views
Last Modified: 2008-02-01
Can I get the filename from a FILE structure or file handle using VC++ 6.0 ? How ?
0
Comment
Question by:Walluce
12 Comments
 
LVL 1

Expert Comment

by:ntdragon
Comment Utility
what do you mean by file structure???
fstream???
or drived from it???
0
 

Author Comment

by:Walluce
Comment Utility
The "FILE" structure, as used in stdout.h e.g. :

FILE *f;
0
 
LVL 9

Expert Comment

by:Pacman
Comment Utility
I think this is os specific. No ANSI solution.
0
 

Author Comment

by:Walluce
Comment Utility
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.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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 *).
0
 

Author Comment

by:Walluce
Comment Utility
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.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 22

Expert Comment

by:nietod
Comment Utility
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.
0
 
LVL 1

Expert Comment

by:ZivKatzir
Comment Utility
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 ?
0
 

Author Comment

by:Walluce
Comment Utility
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.
0
 
LVL 1

Expert Comment

by:mournblade
Comment Utility
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$
0
 
LVL 4

Accepted Solution

by:
saurabh_dasgupta earned 50 total points
Comment Utility
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

4)
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, ..)


5)
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.
0
 

Author Comment

by:Walluce
Comment Utility
I just want to mention that the username password thing was just an example.

Thank you guys for the help;
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

771 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now