How can I convert a file pointer ( FILE* fp ) to a file descriptor (int fd)?
Asked Answered
M

3

217

I have a FILE *, returned by a call to fopen(). I need to get a file descriptor from it, to make calls like fsync(fd) on it. What's the function to get a file descriptor from a file pointer?

Medicament answered 2/7, 2010 at 16:1 Comment(2)
Or you could open the file with open(2), and you would have a file descriptor from the beginningGault
And the inverse: How to get a FILE pointer from a file descriptor?Paramatta
M
271

The proper function is int fileno(FILE *stream). It can be found in <stdio.h>, and is a POSIX standard but not standard C.

Medicament answered 2/7, 2010 at 16:2 Comment(6)
Strictly speaking, there wouldn't be any need to mention any headers or libraries if the function was indeed a part of standard C library. However, it is not standard, which is why it might make sense to mention the header at least.Imbrication
Accessing functions in the standard C library does require including headers, at least if your compiler expects prototypes (I never remember what's actually standard behavior in that respect). Without headers, no names are defined at the beginning of a C file.Medicament
@Novelocrat: I didn't mean that there's no need to #include anything. I merely meant that it is always easy to find the name of the proper header for a standard function. I.e. it is not really critical to mention the exact header name in the answer.Imbrication
This is a good answer, but it is worth noting that this isn't a standard c function, it is a posix function.Arlynearlynne
It is not in the C standard, because file descriptors are a UNIX thing (on Linux, it is open(2), not open(3)).Tojo
Do we need to call the standard C fflush(fp) function first before calling fsync(fd)?Injection
M
53

Even if fileno(FILE *) may return a file descriptor, be VERY careful not to bypass stdio's buffer. If there is buffer data (either read or unflushed write), reads/writes from the file descriptor might give you unexpected results.

To answer one of the side questions, to convert a file descriptor to a FILE pointer, use fdopen(3)

Modeling answered 14/11, 2013 at 5:27 Comment(3)
I don't believe this was helpful to people searching for this specifically, and this has already been answered in the comment section of OP's post a month before this was written. This is more of a logic clarification as opposed to real information.Tremml
Comments that are sooooper important need to be raised as answers. A huge percent of comments are snarky, pointless metacomments like yours...and this one ;)Modeling
@MarkGerolimatos this answer is really helpful but would be even more so if you provided more info. Specifically, if I want to read/write to the file descriptor, how to I avoid bypassing stdio's buffer? Thanks!Seasoning
L
3
  fd = _fileno(fp);  // Probably the best way
       fd = fp->_file;     // direct from the FILE structure, member 

   
    typedef struct _iobuf  {
       char*   _ptr;
       int     _cnt;
       char*   _base;
       int     _flag;
       int     _file;
       int     _charbuf;
       int     _bufsiz;
       char*   _tmpfname; } FILE;
Lymph answered 6/12, 2021 at 2:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.