NAME
fileno – map a stream pointer to a file descriptor
SYNOPSIS
#include <stdio.h>
int fileno(FILE *stream);
DESCRIPTION
The fileno() function shall return the integer file descriptor associated with the stream pointed to by
stream.
RETURN VALUE
Upon successful completion, fileno() shall return the integer value of the file descriptor associated with
stream. Otherwise, the value -1 shall be returned and errno set to indicate the error.
ERRORS
The fileno() function may fail if:
EBADF The stream argument is not a valid stream
EXAMPLES
None.
APPLICATION USAGE
None.
RATIONALE
Without some specification of which file descriptors are associated with these streams, it is impossible for
an application to set up the streams for another application it starts with fork() and exec. In particular, it
would not be possible to write a portable version of the sh command interpreter (although there may be other
constraints that would prevent that portability).
FUTURE DIRECTIONS
None.
相关函数: open, fopen
表头文件:#include <stdio.h>
定义函数: int fileno(FILE *stream)
函数说明:fileno()用来取得参数stream指定的文件流所使用的文件描述词
返回值 :返回文件描述词
范例:
#include <stdio.h>
main()
{
FILE *fp;
int fd;
fp = fopen(“/etc/passwd”, “r”);
fd = fileno(fp);
printf(“fd = %d\n”, fd);
fclose(fp);
}
1.内核(kernel)利用文件描述符(file descriptor)来访问文件。文件描述符是非负整数。打开现存文件或新建文件时,内核会返回一个文件描述符。读写文件也需要使用文件描述符来指定待读写的文件。
习惯上,标准输入(standard input)的文件描述符是 0,标准输出(standard output)是 1,标准错误(standard error)是 2。尽管这种习惯并非 Unix 内核的特性,但是因为一些 shell 和很多应用程序都使用这种习惯,因此,如果内核不遵循这种习惯的话,很多应用程序将不能使用。
POSIX 定义了 STDIN_FILENO、STDOUT_FILENO 和 STDERR_FILENO 来代替 0、1、2。这三个符号常量的定义位于头文件 unistd.h。
文件描述符的有效范围是 0 到 OPEN_MAX。一般来说,每个进程最多可以打开 64 个文件(0 — 63)。对于 FreeBSD 5.2.1、Mac OS X 10.3 和 Solaris 9 来说,每个进程最多可以打开文件的多少取决于系统内存的大小,int 的大小,以及系统管理员设定的限制。Linux 2.4.22 强制规定最多不能超过 1,048,576 。
文件描述符是由无符号整数表示的句柄,进程使用它来标识打开的文件。文件描述符与包括相关信息(如文件的打开模式、文件的位置类型、文件的初始类型等)的文件对象相关联,这些信息被称作文件的上下文。
2. 指向那个文件的指针,指的是你打开的那个文件的指针,也可以说是句柄。
3.当打开或创建一个文件的时候,内核向进程返回一个文件描述符..比如用open函数..creat函数等等.
它并非是定义的.
但是对于标准输入,输出,和错误.其分别用0,1,2标识,同时它们又有相应的常量STDIN_FILENO,STDOUT_FILENO,STDERR_FILENO
4.不唯一.
比如有两个进程.一个读,一个写的.它们打开同一文件,那么对于这个文件来说就有两个文件描述符.
但文件并不知道自己的文件描述符,因为文件描述符是进程所拥有的.
5.struct stat描述了文件的属性..诸如是什么类型的文件,为谁所有,属于哪个组,何时被修改过等等