Export (0) Print
Expand All
div
eof
Expand Minimize

_fileno 

Gets the file descriptor associated with a stream.


int _fileno( 
   FILE *stream 
);

Parameters

stream

Pointer to the FILE structure.

_fileno returns the file descriptor. There is no error return. The result is undefined if stream does not specify an open file. If stream is NULL, _fileno invokes the invalid parameter handler, as described in Parameter Validation. If execution is allowed to continue, this function returns -1 and sets errno to EINVAL.

For more information about these and other error codes, see _doserrno, errno, _sys_errlist, and _sys_nerr.

NoteNote

In Visual C++ 2005, there is a behavior change. If stdout or stderr is not associated with an output stream (for example, in a Windows application without a console window), the file descriptor returned is -2. In previous versions, the file descriptor returned was -1. This change allows applications to distinguish this condition from an error.

The _fileno routine returns the file descriptor currently associated with stream. This routine is implemented both as a function and as a macro. For information about choosing either implementation, see Choosing Between Functions and Macros.

Function Required header Compatibility

_fileno

<stdio.h>

Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003, Windows Server 2003

For more compatibility information, see Compatibility in the Introduction.

// crt_fileno.c
// This program uses _fileno to obtain
// the file descriptor for some standard C streams.
//

#include <stdio.h>

int main( void )
{
   printf( "The file descriptor for stdin is %d\n", _fileno( stdin ) );
   printf( "The file descriptor for stdout is %d\n", _fileno( stdout ) );
   printf( "The file descriptor for stderr is %d\n", _fileno( stderr ) );
}

Output

The file descriptor for stdin is 0
The file descriptor for stdout is 1
The file descriptor for stderr is 2

Community Additions

ADD
Show:
© 2014 Microsoft