Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

File * to fd
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

Report this thread to moderator Post Follow-up to this message
Old Post
Kamal R. Prasad
12-23-04 09:07 PM


Re: File * to fd
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

Report this thread to moderator Post Follow-up to this message
Old Post
Jens.Toerring@physik.fu-berlin.de
12-23-04 09:07 PM


Re: File * to fd
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

Report this thread to moderator Post Follow-up to this message
Old Post
Rich Gibbs
12-23-04 09:07 PM


Re: File * to fd
"Rich Gibbs" <rgibbs@REMOVEalumni.CAPSprinceton.edu> wrote in message news:4
1cb1dba@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,  f
or instance).

Dan Mercer
:
: --
: Rich Gibbs
: rgibbs@alumni.princeton.edu



Report this thread to moderator Post Follow-up to this message
Old Post
Dan Mercer
12-24-04 02:03 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Unix Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 08:38 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.