File * to fd

  • Follow


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
0
Reply kamalp (202) 12/23/2004 6:10:43 PM

kamalp@acm.org (Kamal R. Prasad) writes:

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

No, you access fileno(fp) and not fp->_file.

(fileno(fp) works in all Unix implementation; fp->_file does not)

Casper
-- 
Expressed in this posting are my opinions.  They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.
0
Reply Casper 12/23/2004 6:35:31 PM


Kamal R. Prasad <kamalp@acm.org> wrote:
> From: termbios (meson@techemail.com)
>>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.

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
0
Reply Jens 12/23/2004 7:04:12 PM

Kamal R. Prasad 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.

-- 
Rich Gibbs
rgibbs@alumni.princeton.edu
0
Reply Rich 12/23/2004 7:32:54 PM

"Rich Gibbs" <rgibbs@REMOVEalumni.CAPSprinceton.edu> wrote in message news:41cb1dba@news101.his.com...
: Kamal R. Prasad 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


0
Reply Dan 12/23/2004 9:39:52 PM

4 Replies
175 Views

(page loaded in 0.123 seconds)

Similiar Articles:













7/17/2012 4:27:23 PM


Reply: