Home > Archive > Unix Programming > December 2004 > File * to fd
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Kamal R. Prasad 2004-12-23, 4:07 pm |
| From: termbios (meson@techemail.com)
Subject: File * to fd
View this article only
Newsgroups: comp.unix.programmer
Date: 2004-12-22 15:24:08 PST
>I get a FILE * pointer by calling popen. Now I want to change it into
a
>file descriptor. Is there a function call for it? Like fdopen create
a
>FILE* out of a file descriptor.
FILE (struct __sFILE) has a member
short _file; /* fileno, if Unix descriptor, else -1 */
so, you just need to access (assuming your FILE* is fp) fp->_file to
access the related file descriptor. Im not aware if the member varies
across implementations -but if it doesn't, you save yourself a
function call by accessing the member directly.
regards
-kamal
| |
| Jens.Toerring@physik.fu-berlin.de 2004-12-23, 4:07 pm |
| Kamal R. Pra <kamalp@acm.org> wrote:
> From: termbios (meson@techemail.com)
> a file descriptor. Is there a function call for it? Like fdopen create
> a FILE* out of a file descriptor.
> FILE (struct __sFILE) has a member
> short _file; /* fileno, if Unix descriptor, else -1 */
> so, you just need to access (assuming your FILE* is fp) fp->_file to
> access the related file descriptor. Im not aware if the member varies
> across implementations -but if it doesn't, you save yourself a
> function call by accessing the member directly.
There's no guarantee anywhere that the FILE structure must have a
member called '_file' and that this is the file descriptor asso-
ciated with the file - while fileno() is a well-defined function
required by POSIX. So using '_file' is a horribly stupid idea. All
you get that way is a program that you never know if it's going to
work correctly on the next platform for the dubious benefit of saving
a few nanoseconds at best from time to time. Giving such advice is
like telling a little child to cross a busy street where it is - "That
big lorry is going to stop for you, don't you worry" - instead of going
to the traffic light 5 meters away in order to save a few steps.
Regards, Jens
--
\ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.toerring.de
| |
| Rich Gibbs 2004-12-23, 4:07 pm |
| Kamal R. Pra said the following, on 12/23/04 13:10:
> From: termbios (meson@techemail.com)
> Subject: File * to fd
>
> View this article only
> Newsgroups: comp.unix.programmer
> Date: 2004-12-22 15:24:08 PST
>
>
>
> a
>
>
> a
>
>
>
>
> FILE (struct __sFILE) has a member
> short _file; /* fileno, if Unix descriptor, else -1 */
>
> so, you just need to access (assuming your FILE* is fp) fp->_file to
> access the related file descriptor. Im not aware if the member varies
> across implementations -but if it doesn't, you save yourself a
> function call by accessing the member directly.
>
This is an exceptionally bad idea. What the FILE structure looks like
is _not_ specified for the application to use, but the fileno() function
is part of POSIX. Trying to go around this to save (maybe) a few
machine instructions is just stupid.
--
Rich Gibbs
rgibbs@alumni.princeton.edu
| |
| Dan Mercer 2004-12-23, 9:03 pm |
|
"Rich Gibbs" <rgibbs@REMOVEalumni.CAPSprinceton.edu> wrote in message news:41cb1dba@news101.his.com...
: Kamal R. Pra said the following, on 12/23/04 13:10:
: > From: termbios (meson@techemail.com)
: > Subject: File * to fd
: >
: > View this article only
: > Newsgroups: comp.unix.programmer
: > Date: 2004-12-22 15:24:08 PST
: >
: >
: >>I get a FILE * pointer by calling popen. Now I want to change it into
: >
: > a
: >
: >>file descriptor. Is there a function call for it? Like fdopen create
: >
: > a
: >
: >>FILE* out of a file descriptor.
: >
: >
: >
: > FILE (struct __sFILE) has a member
: > short _file; /* fileno, if Unix descriptor, else -1 */
: >
: > so, you just need to access (assuming your FILE* is fp) fp->_file to
: > access the related file descriptor. Im not aware if the member varies
: > across implementations -but if it doesn't, you save yourself a
: > function call by accessing the member directly.
: >
:
: This is an exceptionally bad idea. What the FILE structure looks like
: is _not_ specified for the application to use, but the fileno() function
: is part of POSIX. Trying to go around this to save (maybe) a few
: machine instructions is just stupid.
Especially if it is defined as a macro - which I have seen (under Cygwin, for instance).
Dan Mercer
:
: --
: Rich Gibbs
: rgibbs@alumni.princeton.edu
|
|
|
|
|