fileno() — Get the file descriptor from an open stream
Standards
Standards / Extensions | C or C++ | Dependencies |
---|---|---|
POSIX.1a |
both |
Format
#define _POSIX_SOURCE
#include <stdio.h>
int fileno(const FILE *stream);
#define _OPEN_SYS_UNLOCKED_EXT 1
#include <stdio.h>
int fileno_unlocked(const FILE *stream);
General description
Returns the file descriptor number associated with a specified z/OS® XL C/C++ I/O stream. The argument stream points to a FILE structure controlling a z/OS XL C/C++ I/O stream.
- STDIN_FILENO
- Standard input, stdin (value 0)
- STDOUT_FILENO
- Standard output, stdout (value 1)
- STDERR_FILENO
- Standard error, stderr (value 2)
Note that stdin, stdout, and stderr are macros, not constants.
fileno_unlocked() is functionally equivalent to fileno() with the exception that it is not thread-safe. This function can safely be used in a multithreaded application if and only if it is called while the invoking thread owns the (FILE*) object, as is the case after a successful call to either the flockfile() or ftrylockfile() function.
Returned value
If successful, fileno() returns the file descriptor number associated with an open HFS stream (that is, one opened with fopen() or freopen()). MVS™ data sets are not supported, so fileno() of an MVS data set returns -1.
- Error Code
- Description
- EBADF
- One of the following error conditions exists:
- stream points to a closed stream
- stream is an incorrect stream pointer
- stream points to a stream associated with an MVS data set.
Example
/* CELEBF21
This example illustrates one use of fileno().
*/
#define _POSIX_SOURCE
#include <errno.h>
#include <stdio.h>
main() {
FILE *stream;
char hfs_file[]="./hfs_file", mvs_ds[]="//mvs.ds";
printf("fileno(stdin) = %d\n", fileno(stdin));
if ((stream = fopen(hfs_file, "w")) == NULL)
perror("fopen() error for HFS file");
else {
printf("fileno() of the HFS file is %d\n", fileno(stream));
fclose(stream);
remove(hfs_file);
}
if ((stream = fopen(mvs_ds, "w")) == NULL)
perror("fopen() error for MVS data set");
else {
errno = 0;
printf("fileno() returned %d for MVS data set,\n",fileno(stream));
printf(" errno=%s\n", strerror(errno));
fclose(stream);
remove(mvs_ds);
}
}
fileno(stdin) = 0
fileno() of the HFS file is 3
fileno() returned -1 for the MVS data set,
errno=Bad file descriptor