convert FILE* to a file descriptor (int)

convert FILE* to a file descriptor (int)

Post by jim_marshall2.. » Tue, 11 Jul 2000 04:00:00



If I have a FILE* and want to convert it to the corresponding file
descriptor, how could I do that?

I know there is a fdopen function, but I need (or would like to) do the
opposite.  specifically I need to do something like this:

FILE* foo  = tmpfile();
int fileSize = writeFile(foo);
mmap((void*)0, fileSize, PROT_READ, MAP_PRIVATE, filedes, 0);

I know there are other ways to do this (tmpnam, open, fdopen) but I was
hoping to do it as above.

BTW I'm on Solaris.

Thanks in adavance

Sent via Deja.com http://www.deja.com/
Before you buy.

 
 
 

convert FILE* to a file descriptor (int)

Post by Phil Edwar » Tue, 11 Jul 2000 04:00:00


+ If I have a FILE* and want to convert it to the corresponding file
+ descriptor, how could I do that?

I'm sure this is a FAQ...

+ BTW I'm on Solaris.

That's not a "by the way," that's a "the most important thing to note is".
The ISO (ANSI) C standard says nothing about file descriptors.  So platforms
and implementations can do it any way they want.  And they don't have
to tell you, or make it available, or keep it the same between releases.
On my Solaris 8 system, a quick glance through header files makes it look
like foo->_file is the file descriptor.  Maybe.  Do you really need that
kind of nonportability?

Phil

 
 
 

convert FILE* to a file descriptor (int)

Post by Casper H.S. Dik - Network Security Engine » Tue, 11 Jul 2000 04:00:00


[[ PLEASE DON'T SEND ME EMAIL COPIES OF POSTINGS ]]


>If I have a FILE* and want to convert it to the corresponding file
>descriptor, how could I do that?
>I know there is a fdopen function, but I need (or would like to) do the
>opposite.  specifically I need to do something like this:
>FILE* foo  = tmpfile();
>int fileSize = writeFile(foo);
>mmap((void*)0, fileSize, PROT_READ, MAP_PRIVATE, filedes, 0);
>I know there are other ways to do this (tmpnam, open, fdopen) but I was
>hoping to do it as above.

The fileno() function/macro returns the fd associated withe a FILE*.

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.

 
 
 

convert FILE* to a file descriptor (int)

Post by Barry Margoli » Tue, 11 Jul 2000 04:00:00





>+ If I have a FILE* and want to convert it to the corresponding file
>+ descriptor, how could I do that?

>I'm sure this is a FAQ...

>+ BTW I'm on Solaris.

>That's not a "by the way," that's a "the most important thing to note is".

Not really.

Quote:>The ISO (ANSI) C standard says nothing about file descriptors.  So platforms

This is comp.unix.programmer, not comp.lang.c, so we're not limited to the
C standard.  The standard we generally care about is POSIX.

Quote:>and implementations can do it any way they want.  And they don't have
>to tell you, or make it available, or keep it the same between releases.
>On my Solaris 8 system, a quick glance through header files makes it look
>like foo->_file is the file descriptor.  Maybe.  Do you really need that
>kind of nonportability?

I believe fileno() is part of POSIX.  You should rarely ever have to access
the _file structure directly.

--

Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

convert FILE* to a file descriptor (int)

Post by Andrew Giert » Tue, 11 Jul 2000 04:00:00


 jim> If I have a FILE* and want to convert it to the corresponding
 jim> file descriptor, how could I do that?

man fileno

--
Andrew.

comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>
                           or <URL: http://www.whitefang.com/unix/>

 
 
 

convert FILE* to a file descriptor (int)

Post by Rob Manchest » Tue, 11 Jul 2000 04:00:00



:
:+ If I have a FILE* and want to convert it to the corresponding file
:+ descriptor, how could I do that?
:
:I'm sure this is a FAQ...
:
:
:+ BTW I'm on Solaris.
:
:That's not a "by the way," that's a "the most important thing to note is".
:The ISO (ANSI) C standard says nothing about file descriptors.  So platforms
:and implementations can do it any way they want.  And they don't have
:to tell you, or make it available, or keep it the same between releases.
:On my Solaris 8 system, a quick glance through header files makes it look
:like foo->_file is the file descriptor.  Maybe.  Do you really need that
:kind of nonportability?

Bad day?  It is true about ansi c but this is comp.unix.programmer and
not comp.lang.c, And in unix there are file discriptors, and to get
the file discriptor, you use the function int fileno(FILE* fp), using
the FILE structure would just be silly, I don't know why you even
mentioned it.  Since fileno is defined in the POSIX standard I would
think that it would be fairly portable, so what is this you are talking
about nonportability?

-r

 
 
 

convert FILE* to a file descriptor (int)

Post by Walter T Rejune » Wed, 12 Jul 2000 04:00:00


Ohhhh, but it can be sooooooo much fun to get in there and diddle
around. I'd probably never do it in a production program, but I have
done experiments for my own edification just to prove that I could do
certain things. There isn't much that can be done in Solaris but in some
operation systems the structure behind FILE allows some interesting
twiddling.

 
 
 

convert FILE* to a file descriptor (int)

Post by Walter T Rejune » Wed, 12 Jul 2000 04:00:00




> :

> :+ If I have a FILE* and want to convert it to the corresponding file
> :+ descriptor, how could I do that?
> :
> :I'm sure this is a FAQ...
> :
> :
> :+ BTW I'm on Solaris.
> :
> :That's not a "by the way," that's a "the most important thing to note is".
> :The ISO (ANSI) C standard says nothing about file descriptors.  So platforms
> :and implementations can do it any way they want.  And they don't have
> :to tell you, or make it available, or keep it the same between releases.
> :On my Solaris 8 system, a quick glance through header files makes it look
> :like foo->_file is the file descriptor.  Maybe.  Do you really need that
> :kind of nonportability?

> Bad day?  It is true about ansi c but this is comp.unix.programmer and
> not comp.lang.c, And in unix there are file discriptors, and to get
> the file discriptor, you use the function int fileno(FILE* fp), using
> the FILE structure would just be silly, I don't know why you even
> mentioned it.  Since fileno is defined in the POSIX standard I would
> think that it would be fairly portable, so what is this you are talking
> about nonportability?

> -r

Keeping in mind that the file descriptor is really the only important
thing that is needed by UNIX since ultimately all functions that perform
I/O via a FILE structure end up using write() or its primitives and they
all use the file descriptor (aka fileno) to identify the output device.
After all FILE is purely a phoney construct that is used to provide
abstration from system calls.
 
 
 

convert FILE* to a file descriptor (int)

Post by Chris Costell » Wed, 12 Jul 2000 04:00:00



Quote:> Keeping in mind that the file descriptor is really the only important
> thing that is needed by UNIX since ultimately all functions that perform
> I/O via a FILE structure end up using write() or its primitives and they
> all use the file descriptor (aka fileno) to identify the output device.
> After all FILE is purely a phoney construct that is used to provide
> abstration from system calls.

   And buffering, not to mention that stdio (the thing that
implements FILE) is ANSI C where the whole file descriptor thing
is POSIX.


 
 
 

convert FILE* to a file descriptor (int)

Post by Barry Margoli » Wed, 12 Jul 2000 04:00:00




Quote:

>Ohhhh, but it can be sooooooo much fun to get in there and diddle
>around. I'd probably never do it in a production program, but I have
>done experiments for my own edification just to prove that I could do
>certain things. There isn't much that can be done in Solaris but in some
>operation systems the structure behind FILE allows some interesting
>twiddling.

But such twiddling would hardly be consistent with someone warning against
non-portable constructs.

--

Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

convert FILE* to a file descriptor (int)

Post by Phil Edwar » Wed, 12 Jul 2000 04:00:00


+ Bad day?

Not yet.

+  It is true about ansi c but this is comp.unix.programmer and
+ not comp.lang.c, And in unix there are file discriptors, and to get
+ the file discriptor, you use the function int fileno(FILE* fp), using

Ah, the wonderful thing about standards is that there are so many to
choose from.  I'd rather make my code portable to everywhere the language
is supported, even if I'm only targeting it to Unix right now.  I apologize
for doing so in comp.unix.programming; next time I'll recommend the direct
use of write(2) instead.  :-|

I like POSIX as much as the next guy, but I've come to distrust it when
working on Solaris (as the original poster said he is).  Generally I write
my code to be standards-conforming, but then I run it through a couple of
compilers on each system and have to go back and take out chunks of POSIX.
POSIX is getting better -- a *lot* better -- but it's still problematic.

Phil

 
 
 

convert FILE* to a file descriptor (int)

Post by Barry Margoli » Wed, 12 Jul 2000 04:00:00





>+ Bad day?

>Not yet.

>+  It is true about ansi c but this is comp.unix.programmer and
>+ not comp.lang.c, And in unix there are file discriptors, and to get
>+ the file discriptor, you use the function int fileno(FILE* fp), using

>Ah, the wonderful thing about standards is that there are so many to
>choose from.  I'd rather make my code portable to everywhere the language
>is supported, even if I'm only targeting it to Unix right now.

stdio is an extremely limited API.  If someone is asking how to get an FD
from a stdio stream, I think it's reasonable to expect them to have a good
reason.  For instance, there's another thread where someone asked how he
can use popen() but not block the program while waiting for the output from
the child process -- the answer was to use fileno() and then use select()
to determine whether output is available.

--

Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

convert FILE* to a file descriptor (int)

Post by Ariel Scolnico » Thu, 13 Jul 2000 04:00:00



[...]

Quote:> Keeping in mind that the file descriptor is really the only important
> thing that is needed by UNIX since ultimately all functions that perform
> I/O via a FILE structure end up using write() or its primitives and they
> all use the file descriptor (aka fileno) to identify the output device.
> After all FILE is purely a phoney construct that is used to provide
> abstration from system calls.

Well, FILE does contain a few other things that are occasionally
useful, such as facilities for buffered I/O.  In particular, this
means that many mixes of read(2)/write(2) to fileno(3) are not a good
idea.

Which is not to say fileno() isn't handy.

--

Compugen Ltd.          |Tel: +972-2-6795059 (Jerusalem) \ We recycle all our Hz
72 Pinhas Rosen St.    |Tel: +972-3-7658514 (Main office)`---------------------
Tel-Aviv 69512, ISRAEL |Fax: +972-3-7658555    http://3w.compugen.co.il/~ariels

 
 
 

1. *FILE and int as file descriptors.

Hi!  I'm confused by the different representation methods of file/stream
descriptors.  For example:

(1) the man page for 'read':
SYNOPSIS
     int read(fd, buf, nbyte)
     int fd;
     char *buf;
     int nbyte;

(2) the man page for 'fopen':
SYNOPSIS
     #include <stdio.h>

     FILE *fopen(filename, type)
     char *filename, *type;

So, why is a file descriptor sometimes 'int', sometimes '*FILE'?
Is 'int' used in original/old systems when integers are not distinguished
from pointers?  

Those things relate to my current problems:

- I opened a serial port /dev/tty03 with fopen() which returns *FILE.  I
  can use fgets() to read in data as if it's a disk file.  Now I need to
  do non-blocking read.  But the fcntl() which sets the non-blocking
  option requires "int" as file descriptor, not *FILE.
  Does that mean I'm stuck here?

- I do error checking as I read data from /dev/tty03.  If an error occurs in
  the middle of transmission, I should immediatly
  send back NAK char to inform the sender to retransmit.  This
  sounds fine, but how do I flush the rest of the data that are still
  pending/buffered on /dev/tty03?  

--

Vincent Q. Yin

2. Probelm with ppp

3. portable way to convert FILE ptr to file descriptor?

4. ProxyPass -> nothing possible?

5. to convert file descriptor to FILE *

6. commercial linux box suppliers

7. int file descriptor to filename?

8. SECURELIB for Sol7 or 2.6?

9. where are files to convert ez files to ps files for printing?

10. buffer flushing; converting file descriptor to stream pointer

11. Converting from unsigned int to int in C++ / More info

12. Converting from unsigned int to int in C++

13. how to get file length from file descriptor