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

Phil Miller picture Phil Miller · Jul 2, 2010 · Viewed 115.2k times · Source

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?

Answer

Phil Miller picture Phil Miller · Jul 2, 2010

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