Get path from FILE* of existing file

A

Alvaro Puente

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
 
D

Dann Corbit

Alvaro Puente said:
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()).
 
D

Dan Pop

In said:
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).



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
 
T

Tom St Denis

Richard said:
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
 
S

skeeter da beeter bunny

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.
 
C

Chris Dollin

Tom said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top