Solved

Filename from FILE struct ?

Posted on 2000-05-03
12
214 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
[X]
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
12 Comments
 
LVL 1

Expert Comment

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

Author Comment

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

FILE *f;
0
 
LVL 9

Expert Comment

by:Pacman
ID: 2772808
I think this is os specific. No ANSI solution.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

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

Expert Comment

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

Author Comment

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

Expert Comment

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

Expert Comment

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

Author Comment

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

Expert Comment

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

Accepted Solution

by:
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

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
ID: 2783581
I just want to mention that the username password thing was just an example.

Thank you guys for the help;
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

635 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