473,499 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Get path from FILE* of existing file

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
5 13233
"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3632
by: Kali K E | last post by:
Hi, I could not understand how I can do the following things in Python. Please help me. 1. First I have to find the current directory from where the script is invoked. 2. Next I have to form a...
31
3866
by: John Roth | last post by:
I'm adding a thread for comments on Gerrit Holl's pre-pep, which can be found here: http://tinyurl.com/2578q Frankly, I like the idea. It's about time that all of the file and directory stuff...
0
1659
by: JP | last post by:
Is there a way to acess .NEt web projects though UNC path (aka network path)? Heres the situation: 1. Dont want to use FP Extentions 2. Can get a web project to open using //IP of server/site...
70
4012
by: Michael Hoffman | last post by:
Many of you are familiar with Jason Orendorff's path module <http://www.jorendorff.com/articles/python/path/>, which is frequently recommended here on c.l.p. I submitted an RFE to add it to the...
34
3221
by: Reinhold Birkenfeld | last post by:
Hi, the arguments in the previous thread were convincing enough, so I made the Path class inherit from str/unicode again. It still can be found in CVS:...
15
11514
by: (Pete Cresswell) | last post by:
I've got a .BAT file that I use for executing various MS Access apps that I wrote way back in the days of 2.0. It's evolved over time, but it still contains a number of possible paths to...
2
1661
by: theinvisibleGhost | last post by:
Subject says it all really. I'm being passed a string, and I'm wondering if theres an easy way to determine what exactly the string points at? Cheers Chris.
7
2936
by: ApexData | last post by:
Hello I currently Link the FE/BE using the LinkTables Option and the Linked Table Manager. Any time I need to move the BE to another location, I have to go through this process over again. I...
4
6873
by: Vlad | last post by:
I am having problems using the file.create method within a function that is called when looping through an array of filepaths. If I call my function with a hardcoded file path --C:\Temp.txt the...
7
1657
by: sdanda | last post by:
I am working on windows applications(VB.net). I created a form with a textbox and one button. I would like to test the value entered in the textbox after this textbox loses the focus. The value...
0
7132
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7009
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
7223
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7390
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...
1
4919
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...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.