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: translations of creat() and write() to ISO C - comp.unix ...> } > > ssize_t write(int fd, const void *buf, size_t count) > { > FILE *output_file = fdopen(fd, "a"); The same goes for fdopen(). So what's the point of this? Does directio(fd, DIRECTIO_ON) survive the file descriptor? - comp ...The directio(3C) man page says "The advice argument is kept per file; the last caller of directio() sets the advice for all applications... How does FD_ISSET() return 0 after returned non-zero? - comp.unix ...----- FD_ISSET(fd, &fdset) Returns a non-zero value if the bit for the file descriptor fd is set in the file descriptor set ... select() and file descriptor usage troubles - comp.unix.programmer ...I have added two serial port file descriptors and the console file descriptor (obtained with fileno(stdin)) to an fd_set. As long as I get input on ONLY ONE of these ... fixed pdf problem now I have a Yap problem - comp.text.tex ...Note: umvs.fd is in directory indicated in log file but Yap says: 1. MiTex Problem Report: Not all fonts could be loaded. 2) when I look in doc properties it ... /dev/fd error message on Netra T1 105 - comp.unix.solaris ...Any selecting of this just repeats the above message on /dev/fd. Return does also. ... This message had nothing to do with a floppy, but refers to a file descriptor device ... Mysterious file descriptor behavior - comp.unix.programmer ...Next, they dup() the opened file (which was given fd 0 - stdin) to get stdout and dup() it again to get stderr. The end result is that you can read from stdin and write ... Problem sending binary file via socket. - comp.unix.programmer ...Hi all, My client code are: send(fd, argv[2], strlen(argv[2]) + 1, 0); fp = fopen(argv[2], "rb"); while (bytes_read = fread(buf, sizeof(char), MAX_D... Number of threads per process - comp.unix.solarisI know that the number of file descriptors is relevant, but setting rlim_fd_cur and rlim_fd_max higher doesn't help in this case. I couldn't find any relevant info on ... Writing non ASCII text files - comp.lang.tcl... 8bit ASCII characters, internally hold as # UTF-8 encoded characters # set file [file join $env(TEMP) utf8Test.txt] set fd [open $file wb ... File Extension .FD DetailsThe FD file type is primarily associated with 'DataFlex' by Data Access Corporation. Other applications associated with file type FD: Final Draft (Document File) by ... 7/17/2012 4:27:23 PM
|