Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this messageKamal 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
Post Follow-up to this messageKamal R. Prasaid 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
Post Follow-up to this message"Rich Gibbs" <rgibbs@REMOVEalumni.CAPSprinceton.edu> wrote in message news:4 1cb1dba@news101.his.com... : Kamal R. Prasaid 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, f or instance). Dan Mercer : : -- : Rich Gibbs : rgibbs@alumni.princeton.edu
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.