By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,549 Members | 1,717 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,549 IT Pros & Developers. It's quick & easy.

Get path from FILE* of existing file

P: n/a
Hi all!

I've got a FILE* pointing to an already opened file.
I want to create a new file in the same directory where
that file is.

Is there any way of obtaining file path from FILE*?

Many thanks/Alvaro
Nov 13 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Alvaro Puente" <al********************@ece.ericsson.se> wrote in message
news:be**********@newstree.wise.edt.ericsson.se...
Hi all!

I've got a FILE* pointing to an already opened file.
I want to create a new file in the same directory where
that file is.

Is there any way of obtaining file path from FILE*?


From the C FAQ:

19.15: How can I recover the file name given an open stream or file
descriptor?

A: This problem is, in general, insoluble. Under Unix, for
instance, a scan of the entire disk (perhaps involving special
permissions) would theoretically be required, and would fail if
the descriptor were connected to a pipe or referred to a deleted
file (and could give a misleading answer for a file with
multiple links). It is best to remember the names of files
yourself as you open them (perhaps with a wrapper function
around fopen()).
--
C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
"The C-FAQ Book" ISBN 0-201-84519-9
C.A.P. FAQ: ftp://cap.connx.com/pub/Chess%20Anal...ject%20FAQ.htm

Nov 13 '05 #2

P: n/a
In <3f***************@news.nl.net> rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
Tom St Denis <to********@iahu.ca> wrote:
Alvaro Puente wrote:
> I've got a FILE* pointing to an already opened file.
> I want to create a new file in the same directory where
> that file is.
>
> Is there any way of obtaining file path from FILE*?

No, there isn't; not in ISO C, anyway. If your implementation needs to
support directories, there is probably a way to get this information,
but it's going to be system-specific and therefore beyond the scope of
this group. Ask in a newsgroup dedicated to your compiler/library/OS
(e.g., microsoft.programming.visual-c.whatever or comp.unix.programmer).
As in the "size of array" case SAVE THE NAME WHEN YOU OPEN IT.

See if you newbs just spent half a minute thinking out your application
you wouldn't run into these traps.


That's all very nice if you just opened the file yourself, but what if
the FILE * was passed in from someone else's code?


Request that someone else to also pass the file name. After all, this
information *does* exist. Tom is right here.
Not all of us hack
toy code for our own amusement, you know. Some of us have to work in the
real world, where we don't always control 100% of the code.


This doesn't prevent us from asking whomever controls the rest of the code
to do the right thing. There is no point in jumping through hoops to get
a piece of information that is readily available in another part of the
program.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #3

P: n/a
Richard Bos wrote:
As in the "size of array" case SAVE THE NAME WHEN YOU OPEN IT.


See if you newbs just spent half a minute thinking out your application
you wouldn't run into these traps.

That's all very nice if you just opened the file yourself, but what if
the FILE * was passed in from someone else's code? Not all of us hack
toy code for our own amusement, you know. Some of us have to work in the
real world, where we don't always control 100% of the code.


Yeah, I "hack" toy code that people from SCEA, Intel, HP, Motorola,
Harvard, MIT and Cisco check out on a semi-regular basis.

While admitedly my projects are mostly pass times and don't make
millions of dollars serious people do at least look at it [I know SCEA
uses it at least] and I try my best to write professional code that
people can use.

As Dan said, get the rest of the developers to write real code.

Tom

Nov 13 '05 #4

P: n/a
I know there was a historical ( hysterical? ) reason while K&R did not put
the path to the file in the FILE * structure, but it escapes me right now.
"Tom St Denis" <to********@iahu.ca> wrote in message
news:1g*******************@news01.bloor.is.net.cab le.rogers.com...
Alvaro Puente wrote:
Hi all!

I've got a FILE* pointing to an already opened file.
I want to create a new file in the same directory where
that file is.

Is there any way of obtaining file path from FILE*?


As in the "size of array" case SAVE THE NAME WHEN YOU OPEN IT.

See if you newbs just spent half a minute thinking out your application
you wouldn't run into these traps.

Tom

Nov 13 '05 #5

P: n/a
Tom St Denis wrote:
Alvaro Puente wrote:
Hi all!

I've got a FILE* pointing to an already opened file.
I want to create a new file in the same directory where
that file is.

Is there any way of obtaining file path from FILE*?
As in the "size of array" case SAVE THE NAME WHEN YOU OPEN IT.


Who says *they* opened the file in the first place?
See if you newbs just spent half a minute thinking out your application
you wouldn't run into these traps.


No; they'd run into different ones instead.

Programs, problems.

--
Chris "everything changes, yet everything keeps changing" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 13 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.