文件描述符和文件指针有什么区别?
文件描述符和文件指针有何关系?什么时候适合分别使用?
How are file descriptors and file pointers related? When is it appropriate to use each?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
文件描述符和文件指针有何关系?什么时候适合分别使用?
How are file descriptors and file pointers related? When is it appropriate to use each?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(9)
文件描述符是一个低级整数“句柄”,用于在 Linux 和其他类 Unix 系统中识别内核级别打开的文件(或套接字或其他任何内容)。
您将“裸”文件描述符传递给实际的 Unix 调用,例如
read()
、write()
等等。FILE
指针是 C 标准库级构造,用于表示文件。FILE
包装文件描述符,并添加缓冲和其他功能以使 I/O 更容易。您将
FILE
指针传递给标准 C 函数,例如fread()
和fwrite()
。A file descriptor is a low-level integer "handle" used to identify an opened file (or socket, or whatever) at the kernel level, in Linux and other Unix-like systems.
You pass "naked" file descriptors to actual Unix calls, such as
read()
,write()
and so on.A
FILE
pointer is a C standard library-level construct, used to represent a file. TheFILE
wraps the file descriptor, and adds buffering and other features to make I/O easier.You pass
FILE
pointers to standard C functions such asfread()
andfwrite()
.一个是缓冲的 (
FILE *
),另一个不是。实际上,当您从“真实”文件(即驱动器上)读取时,您几乎总是希望使用FILE *
,除非您知道自己在做什么,或者除非您的文件实际上是一个 从您可以使用
fileno()
从FILE *
获取文件描述符,并且可以 使用 fdopen() 的文件描述符One is buffered (
FILE *
) and the other is not. In practice, you want to useFILE *
almost always when you are reading from a 'real' file (ie. on the drive), unless you know what you are doing or unless your file is actually a socket or so..You can get the file descriptor from the
FILE *
usingfileno()
and you can open a bufferedFILE *
from a file descriptor usingfdopen()
文件描述符只是一个从 POSIX
open()
调用中获得的整数。使用标准 Cfopen()
你会得到一个FILE
结构体。FILE
结构包含此文件描述符以及其他内容,例如文件结束和错误指示符、流位置等。因此,使用
fopen()
可以为您提供一定量的与open()
相比的抽象。一般来说,您应该使用fopen()
因为它更便携,并且您可以使用使用FILE
结构的所有其他标准 C 函数,即fprintf ()
和家人。使用两者都不存在性能问题。
A file descriptor is just an integer which you get from the POSIX
open()
call. Using the standard Cfopen()
you get aFILE
struct back. TheFILE
struct contains this file descriptor amongst other things such as end-of-file and error indicator, stream position etc.So using
fopen()
gives you a certain amount of abstraction compared toopen()
. In general you should be usingfopen()
since that is more portable and you can use all the other standard C functions that uses theFILE
struct, i.e.,fprintf()
and family.There are no performance issues using either.
文件描述符与文件指针
文件描述符:
文件描述符是由
open()
系统调用返回的整数值。int fd = open (filePath, mode);
文件指针:
文件指针是指向
fopen()
库函数返回的C结构体的指针,用于标识一个文件,包装了文件描述符、缓冲功能以及I/O操作所需的所有其他功能。文件指针的类型为FILE,其定义可以在"/ usr/include/stdio.h"。这个定义可能因编译器的不同而不同。File descriptor vs File pointer
File descriptor:
File Descriptor is an integer value returned by
open()
system call.int fd = open (filePath, mode);
File pointer:
File Pointer is a pointer to a C structure returned by
fopen()
library function, which is used to identifying a file, wrapping the file descriptor, buffering functionality and all other functionality needed for I/O operation.The file pointer is of type FILE, whose definition can be found in "/usr/include/stdio.h". This definition may vary from one compiler to another.想添加可能有用的点。
ABOUT
FILE *
我多次使用它来记录调试日志。
例如,
<前><代码> 文件 *fp;
fp = fopen("debug.txt","a");
fprintf(fp,"我已经达到了这一点");
fclose(fp);
ABOUT
FILE DESCRIPTOR
一般用于IPC。
FILE *
更强大。Want to add points which might be useful.
ABOUT
FILE *
I use it many times for debug logs.
example,
ABOUT
FILE DESCRIPTOR
It's generally used for IPC.
Gives low-level control to files on *nix systems.(devices,files,sockets,etc), hence more powerfull than the
FILE *
.当您处理文本文件和用户输入/输出时,
FILE *
更有用,因为它允许您使用sprintf()
、sscanf() 等 API 函数
、fgets()
、feof()
等。文件描述符 API 是低级的,因此它允许使用套接字、管道、内存映射文件(当然还有常规文件)。
FILE *
is more useful when you work with text files and user input/output, because it allows you to use API functions likesprintf()
,sscanf()
,fgets()
,feof()
etc.File descriptor API is low-level, so it allows to work with sockets, pipes, memory-mapped files (and regular files, of course).
只是结束讨论的注释(如果有兴趣)......
fopen
可能不安全,您应该使用fopen_s
或open
具有专属位设置。 C1X 提供x
模式,因此您可以使用"rx"
、"wx"
等模式fopen
。如果您使用
open
,您可能会考虑open(..., O_EXCL | O_RDONLY,... )
或open(..., O_CREAT | O_EXCL | O_WRONLY,...)
。例如,请参阅不要对 fopen() 和文件创建做出假设。
Just a note to finish out the discussion (if interested)....
fopen
can be insecure, and you should probably usefopen_s
oropen
with exclusive bits set. C1X is offeringx
modes, so you canfopen
with modes"rx"
,"wx"
, etc.If you use
open
, you might consideropen(..., O_EXCL | O_RDONLY,... )
oropen(..., O_CREAT | O_EXCL | O_WRONLY,... )
.See, for example, Do not make assumptions about fopen() and file creation.
我在这里找到了一个很好的资源 ,高度概括了两者之间的差异:
I found a good resource here, giving high level overview of differences between the two:
系统调用主要使用文件描述符,例如
read
和write
。库函数将使用文件指针(printf
、scanf
)。但是,库函数仅使用内部系统调用。System calls are mostly using file descriptor, for example
read
andwrite
. Library function will use the file pointers (printf
,scanf
). But, library functions are using internally system calls only.