fileno()

Updated: April 19, 2023

Return the file descriptor for a stream

Synopsis:

#include <stdio.h>

int fileno( FILE * stream );

Arguments:

stream
The stream whose file descriptor you want to find.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The fileno() function returns the file descriptor for the specified file stream. This file descriptor can be used in POSIX input/output calls anywhere the value returned by open() can be used.

To associate a stream with a file descriptor, call fdopen().

Note: In QNX Neutrino, the file descriptor is also the connection ID (coid) used by various QNX Neutrino-specific functions.

The following symbolic values in <unistd.h> define the file descriptors associated with the C language stdin, stdout, and stderr streams:

STDIN_FILENO
Standard input file number, stdin (0)
STDOUT_FILENO
Standard output file number, stdout (1)
STDERR_FILENO
Standard error file number, stderr (2)

Returns:

A file descriptor, or -1 if an error occurs (errno is set).

Examples:

#include <stdlib.h>
#include <stdio.h>

int main( void )
{
    FILE *stream;

    stream = fopen( "file", "r" );
    if( stream != NULL ) {
        printf( "File number is %d.\n", fileno( stream ) );
        fclose( stream );
        
        return EXIT_SUCCESS;
    }
    
    return EXIT_FAILURE;
}

This program produces output similar to:

File number is 7.

Classification:

POSIX 1003.1

Safety:  
Cancellation point No
Interrupt handler No
Signal handler Yes
Thread Yes