HAVE: FILE *, WANT: filename

P

Pelle

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 know I could be passing the filename from the
calling function, but since I don't know which entry to the toolbox I
took (e.g. toolbox-functions calling each other) it would be far
preferable
to query the filename of the file from the FILE * pointer that I
have.

Oh, and of course I'd need to find out if FILE * points to an actual
file or maybe something else.

Does anyone know how to do this?


Thanks in advance,
regards,
Pelle.
 
V

vippstar

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 know I could be passing the filename from the
calling function, but since I don't know which entry to the toolbox I
took (e.g. toolbox-functions calling each other) it would be far
preferable
to query the filename of the file from the FILE * pointer that I
have.

Oh, and of course I'd need to find out if FILE * points to an actual
file or maybe something else.

Does anyone know how to do this?

Not possible in standard C.
Jacob Navia maintains a compiler that offers this as an extension.
<http://www.cs.virginia.edu/~lcc-win32/>

There's possibly other compilers with such extensions. Discussing
those extensions, or the compilers here is off-topic.
 
R

Richard

Yes. Compile and link with a debug version of the libraries in question
and investigate in the debugger. Or use a package like tags or etags or
cscope in order to find the definition of your implementations FILE
structure. Possibly what you want is stored for you. Possibly not. The
you get into implementation specific regions and you are better off
asking the provider of your compiler or experts in a forum/group
dedicated to that particular version.
Not possible in standard C.

Nonsense. It is totally possible in C. What do you think the
implementations headers are? Pascal? There might not be a function to
help him but if you think

"pFile->szFileName" is not standard C then there is a problem

myStruct is not standard C either ....
Jacob Navia maintains a compiler that offers this as an extension.
<http://www.cs.virginia.edu/~lcc-win32/>

There's possibly other compilers with such extensions. Discussing
those extensions, or the compilers here is off-topic.

For someone learning C you sure didn't take long to start strutting
around telling people what they can and can not discuss.
 
C

CBFalconer

Pelle said:
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 know I could be
passing the filename from the calling function, but since I don't
know which entry to the toolbox I took (e.g. toolbox-functions
calling each other) it would be far preferable to query the
filename of the file from the FILE * pointer that I have.

It is not possible. Unix and Unix clones are one of the major
areas running C programs. The file system is such that the file
name can disappear, or be changed, while the file is open. The
program using the file never knows about these details, and
functions correctly.
 
F

Flash Gordon

Richard wrote, On 12/09/08 12:43:
Yes. Compile and link with a debug version of the libraries in question
and investigate in the debugger.

Assuming that debug versions of all the relevant code is available.
Or use a package like tags or etags or
cscope in order to find the definition of your implementations FILE
structure. Possibly what you want is stored for you. Possibly not. The
you get into implementation specific regions and you are better off
asking the provider of your compiler or experts in a forum/group
dedicated to that particular version.

So here you are giving exactly the advice you complain about vippstar
giving, i.e. ask in a group dedicated to the specific implementation.
Nonsense. It is totally possible in C. What do you think the
implementations headers are? Pascal?

They can be implemented in any way the implementor wants, there is no
requirement for them to be actual files let alone files containing C.
They commonly are, but it is not required.
There might not be a function to
help him but if you think

"pFile->szFileName" is not standard C then there is a problem

Strangely enough the definition of the FILE structure on my system here
does not include a pointer to the file name. I just looked. So if pFile
is defined as a pointer to FILE it is NOT standard. If, on the other
hand, it is a pointer to a user defined structure which has a field
named szFileName it is entirely standard.
myStruct is not standard C either ....

It is an identifier the C standard allows the user to define. Nothing
non-standard about it unless you are assuming the implementation
provides it.
For someone learning C you sure didn't take long to start strutting
around telling people what they can and can not discuss.

Perhaps he is a faster learner than you.
 
P

Pelle

Dear all,

thank you for taking the time to shed some light on the problem as
well as highlighting additional problems that arise with it. I think
I'll have to skip on this one - since I'm not looking for a compiler-
specific way to do this.

Anyway,
thanks for your time and help, it's been appreciated!

Regards,
Pelle.
 
N

Nick Keighley

Dear all,

thank you for taking the time to shed some light on the problem as
well as highlighting additional problems that arise with it. I think
I'll have to skip on this one - since I'm not looking for a compiler-
specific way to do this.

Anyway,
thanks for your time and help, it's been appreciated!

Note the poster who posts under the name "Richard" sometimes
gives good advice and sometimes merely knocks other people and
generally tries to provoke a reaction.
Today he is having a troll day so just ignore him.
 
C

CBFalconer

Nick said:
.... snip ...

Note the poster who posts under the name "Richard" sometimes
gives good advice and sometimes merely knocks other people and
generally tries to provoke a reaction.
Today he is having a troll day so just ignore him.

He posts as "Richard<[email protected]>" (without the quotes), if
you want to PLONK his annoyances.
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top