473,403 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

how to get the filename of a FILE pointer

Hi all:
I have a FILE pointer that points to a file on disk (not any other io
stream). is there a way to get the filename of that FILE?
i thought it would be the _tmpfname part of the struct, but it doesn't look
like it

thanks
hilz

Nov 14 '05 #1
11 26134
"hilz" <no****@nowhere.com> writes:
I have a FILE pointer that points to a file on disk (not any other io
stream). is there a way to get the filename of that FILE?


Not portably.
--
"When I have to rely on inadequacy, I prefer it to be my own."
--Richard Heathfield
Nov 14 '05 #2
>I have a FILE pointer that points to a file on disk (not any other io
stream). is there a way to get the filename of that FILE?
i thought it would be the _tmpfname part of the struct, but it doesn't look
like it


This is highly system-dependent and likely very expensive. On some
systems, you can do a file-tree walk, stat() each file looking for
one with a matching device and inode numbers. BUT: there's no
guarantee that a file still has a name. (unlinking an open file
is not guaranteed to destroy the file). There's no guarantee that
a file has only one name (even if you don't follow symlinks). And
even if it does have a name, there's no guarantee that you have
permission to use it or even determine it. And, of course, there's
no guarantee that your system even HAS stat(), symlinks, device
numbers, inode numbers, opendir(), readdir(), or unlink().

File-tree walks on some systems (particularly large news servers
with terabytes of storage) can take days. This is not something
you want to do lightly.

If you fopen()ed the file yourself, SAVE THE NAME if you're going
to need it. If the FILE in question is stdin, stdout, or stderr,
maybe the program interface should be changed so you supply a
filename instead.

Gordon L. Burditt
Nov 14 '05 #3
hilz wrote:
I have a FILE pointer that points to a file on disk
(not any other I/O stream).
is there a way to get the filename of that *FILE?
No.
I thought it would be the _tmpfname part of the struct,
but it doesn't look like it

Nov 14 '05 #4
You cannot get the filename. What if the file has two names?

hilz wrote:
Hi all:
I have a FILE pointer that points to a file on disk (not any other io
stream). is there a way to get the filename of that FILE?
i thought it would be the _tmpfname part of the struct, but it doesn't
look like it

thanks
hilz


--
remove .nospam from e-mail address to reply
Nov 14 '05 #5

"Gordon Burditt" <go****@hammy.burditt.org> wrote in message
news:ck********@library2.airnews.net...
I have a FILE pointer that points to a file on disk (not any other io
stream). is there a way to get the filename of that FILE?
i thought it would be the _tmpfname part of the struct, but it doesn't looklike it


This is highly system-dependent and likely very expensive. On some
systems, you can do a file-tree walk, stat() each file looking for
one with a matching device and inode numbers. BUT: there's no
guarantee that a file still has a name. (unlinking an open file
is not guaranteed to destroy the file). There's no guarantee that
a file has only one name (even if you don't follow symlinks). And
even if it does have a name, there's no guarantee that you have
permission to use it or even determine it. And, of course, there's
no guarantee that your system even HAS stat(), symlinks, device
numbers, inode numbers, opendir(), readdir(), or unlink().

File-tree walks on some systems (particularly large news servers
with terabytes of storage) can take days. This is not something
you want to do lightly.

If you fopen()ed the file yourself, SAVE THE NAME if you're going
to need it. If the FILE in question is stdin, stdout, or stderr,
maybe the program interface should be changed so you supply a
filename instead.

Gordon L. Burditt

The problem is that the FILE pointer is supplied to me by a third party
function that i have no control over, and there is no way i can get the name
from anywhere else. So unfortunately, i cannot "SAVE THE NAME".

In addition, I am working exclusively with Window$ where there is only one
file with that name and path.
does that make it possible now?

thanks
hilz
Nov 14 '05 #6
"hilz" <no****@nowhere.com> writes:
In addition, I am working exclusively with Window$ where there is only one
file with that name and path.
does that make it possible now?


Asking Windows questions in Windows newsgroups improves your
chances at a useful response.
--
Ben Pfaff
email: bl*@cs.stanford.edu
web: http://benpfaff.org
Nov 14 '05 #7
> Asking Windows questions in Windows newsgroups improves your
chances at a useful response.


i thought this was more of a C question than a Windows question.
If not, i appologize.


Nov 14 '05 #8
hilz wrote:

I have a FILE pointer that points to a file on disk (not any other
io stream). is there a way to get the filename of that FILE?
i thought it would be the _tmpfname part of the struct, but it
doesn't look like it


You had the filename when you fopened the file. Use that.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #9
>> >I have a FILE pointer that points to a file on disk (not any other io
>stream). is there a way to get the filename of that FILE?
>i thought it would be the _tmpfname part of the struct, but it doesn'tlook >like it
This is highly system-dependent and likely very expensive. On some
systems, you can do a file-tree walk, stat() each file looking for
one with a matching device and inode numbers. BUT: there's no
guarantee that a file still has a name. (unlinking an open file
is not guaranteed to destroy the file). There's no guarantee that
a file has only one name (even if you don't follow symlinks). And
even if it does have a name, there's no guarantee that you have
permission to use it or even determine it. And, of course, there's
no guarantee that your system even HAS stat(), symlinks, device
numbers, inode numbers, opendir(), readdir(), or unlink().

File-tree walks on some systems (particularly large news servers
with terabytes of storage) can take days. This is not something
you want to do lightly.

If you fopen()ed the file yourself, SAVE THE NAME if you're going
to need it. If the FILE in question is stdin, stdout, or stderr,
maybe the program interface should be changed so you supply a
filename instead.

Gordon L. Burditt

The problem is that the FILE pointer is supplied to me by a third party
function that i have no control over, and there is no way i can get the name
from anywhere else. So unfortunately, i cannot "SAVE THE NAME".


This depends on how badly you need it. Sometimes you need it so
bad you can afford to not only scrap or replace the existing system,
but also its OS, the entire office staff, the building, and the
whole site (meaning land, not hostname) and perhaps even a whole
country. Or, you could BUY the third party. Microsoft does that
a lot. And it might even be faster.
In addition, I am working exclusively with Window$ where there is only one
file with that name and path.
Yes, but is there only one name and path for that file? I thought
Windows had long and short names for just about every file and
directory, therefore making many names for the same file very common.
does that make it possible now?


Possible, maybe, fast, unlikely, portably, no chance. If you
don't insist on a unique filename, just at least one that works
when running with Administrator privilege, probably.

Does your compiler have stat() for Windows? My guess is that it
probably does. Does your compiler have something that can do the
equivalent of stat() on an open stream? (e.g. on UNIX, fstat() on
fileno(stream)). A bit less obvious, but probably available. Does
stat() return device and inode numbers that *UNIQUELY IDENTIFY A
FILE*? (as distinguished from dummy numbers). This I am not sure
about. I don't think Windows filesystems HAVE inode numbers and
I'm not sure they have any kind of unique file number.

You're trying to match these numbers from fstat() on the open stream
against stat() on files in the filesystem to find a match. If they
are dummy, forget it, many unrelated files will match.

How long does it take for you to get Windows to scan your whole
hard disk to look for files of a particular type (using, for example,
the Windows "Find a File" GUI)? This is probably how long a full
tree-walk will take. Is this acceptable speed? On a bare-bones
Windows XP system (just about the only application installed or
ever used is Windows Update) with about 8GB of disk space, this
took 5 minutes (certainly unacceptable for a Web application. Maybe
no problem for a batch job). How much this takes on your system
will depend on whether your disk is bigger than your processor or
vice versa.

Gordon L. Burditt
Nov 14 '05 #10
In article <3N09d.12041$na.8192@trnddc04> "hilz" <no****@nowhere.com> writes:
Asking Windows questions in Windows newsgroups improves your
chances at a useful response.


i thought this was more of a C question than a Windows question.
If not, i appologize.


You have gotten your C answer. It can not be done in standard C.
Whether it is possible on your platform is a different question,
and that is indeed a Windows question, so platform dependent.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 14 '05 #11
hilz wrote:
[...]
The problem is that the FILE pointer is supplied to me by a third party
function that i have no control over, and there is no way i can get the name
from anywhere else. So unfortunately, i cannot "SAVE THE NAME".

In addition, I am working exclusively with Window$ where there is only one
file with that name and path.
does that make it possible now?


I know there is some way under Windows to go from handle to filename, as
I have utilities that display all open files in a process, including the
handle and filename, among other information. However, this is highly
Windows-specific.

How to do that, I couldn't say, but one of the Windows newsgroups would
be a good place to start. Perhaps "comp.os.ms-windows.programmer.win32"?

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+

Nov 14 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: S.W. Rasmussen | last post by:
With the risk of being accused of multi-posting I would like to draw the attention to a serious visual basic/windows issue discussed in the microsoft.public.vb.bugs newsgroup. As pointed out below...
5
by: Spauldo da Hippie | last post by:
Hello, I'm currently writing a cgi program in perl (target platform will be win32/IIS) that will act as a fileserver. One of the scripts will display a page with the files available to that...
5
by: George | last post by:
This program need to draw the some triangles into a 512 × 512 buffer (in memory). Or save it to a file. #include "project3.h" Image::Image(int xres, int yres): xres(xres), yres(yres) {...
6
by: varlagas | last post by:
We disabled the antivirus software but the problem persists. Any clues? Many thanks in advance! Panagiotis Varlagas ======================================================================= ...
9
by: BMarsh | last post by:
Hi all, I have the following piece of code which i've been wrestling with for sometime, which I attribute, still, to my lack of understanding of pointers. (this is probably rining alarm bells...
27
by: gmtonyhoyt | last post by:
I need assistance coming up with a clean way to handle filename extensions with my application. While I can come up with several ways of doing so on my own, I felt perhaps it would be worth...
8
by: bobrics | last post by:
Hi, I would like to save data from multiple repetitive simulations into a set of files. Each time a simulation restarts, I would like it to automatically generate a file with a name containing...
10
by: Mark | last post by:
let's say i have a string "c:\hello.txt" and i just want the "c:\hello" part, or just "hello" (so that i can append something to the end of the filename) i can use strrchr("c:\hello.txt", '.')...
5
by: Pelle | last post by:
Hello programmers, I have a toolbox that's often working with a FILE * to access files and the lot. Recently, need has arisen to look at the filename of a file when I only have a FILE *. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.