文件描述符和文件指针之间的区别是什么?

时间:2022-01-11 03:40:05

I want to know the difference between a file descriptor and file pointer.

我想知道文件描述符和文件指针之间的区别。

Also, in what scenario would you use one instead of the other?

还有,在什么情况下你会用一个代替另一个?

9 个解决方案

#1


113  

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.

文件描述符是一个低级整数“句柄”,用于在Linux和其他类unix系统的内核级标识打开的文件(或套接字或其他)。

You pass "naked" file descriptors to actual Unix calls, such as read(), write() and so on.

将“裸”文件描述符传递给实际的Unix调用,例如read()、write()等等。

A FILE pointer is a C standard library-level construct, used to represent a file. The FILE wraps the file descriptor, and adds buffering and other features to make I/O easier.

文件指针是C标准库级结构,用于表示文件。该文件包装文件描述符,并添加缓冲和其他特性以使I/O更容易。

You pass FILE pointers to standard C functions such as fread() and fwrite().

将文件指针传递给标准的C函数,如fread()和fwrite()。

#2


49  

One is buffered (FILE *) and the other is not. In practice, you want to use FILE * 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..

一个是缓冲(文件*),另一个不是。在实践中,当您从一个“真实”文件(例如)中读取文件时,您几乎总是希望使用FILE *。在驱动器上),除非你知道你在做什么或除非你的文件实际上是一个套接字左右。

You can get the file descriptor from the FILE * using fileno() and you can open a buffered FILE * from a file descriptor using fdopen()

可以使用fileno()从文件*获得文件描述符,也可以使用fdopen()从文件描述符*打开缓冲文件*

#3


13  

A file descriptor is just an integer which you get from the Posix' open()call. Using the standard C fopen() you get a FILE struct back. The FILE struct contains the this file descriptor amongst other things such as end-of-file and error indicator, stream position etc.

文件描述符只是从Posix' open()调用中获得的一个整数。使用标准的C fopen()可以得到一个文件结构。文件结构体包含这个文件描述符以及诸如文件结束、错误指示符、流位置等等。

So using fopen() gives you a certain amount of abstraction compared to open(). In general you should be using fopen() since that is more portable and you can use all the other standard C functions that uses the FILE struct, ie fprintf() and family.

因此,与open()相比,使用fopen()可以提供一定的抽象。一般来说,您应该使用fopen(),因为它更便于携带,您可以使用所有使用文件结构的标准C函数,即fprintf()和家庭。

There are no performance issues using either or.

使用or时没有性能问题。

#4


9  

Want to add points which might be useful.

想要添加一些有用的点。

ABOUT FILE *

关于文件*

  1. can't be used for interprocess communication(IPC).
  2. 不能用于进程间通信(IPC)。
  3. use it when you need genral purpose buffered I/O.(printf,frpintf,snprintf,scanf)
  4. 当您需要genral目的缓冲I/ o时使用它(printf、frpintf、snprintf、scanf)
  5. I use it many times for debug logs. example,

    我多次使用它来调试日志。的例子,

                 FILE *fp;
                 fp = fopen("debug.txt","a");
                 fprintf(fp,"I have reached till this point");
                 fclose(fp);
    

ABOUT FILE DESCRIPTOR

关于文件描述符

  1. It's generally used for IPC.

    它通常用于IPC。

  2. Gives low-level control to files on *nix systems.(devices,files,sockets,etc), hence more powerfull than the FILE *.

    对*nix系统上的文件进行低级控制(设备、文件、套接字等),因此比文件*更强大。

#5


7  

File descriptor vs File pointer

文件描述符vs文件指针。

File descriptor:

文件描述符:

File Descriptor is an integer value returned by open() system call.

文件描述符是open()系统调用返回的整数值。

int fd = open (filePath, mode);

int fd = open (filePath, mode);

  1. Low/Kernel level handler.
  2. 低/内核级别的处理程序。
  3. passe to read() and write() of UNIX System Calls.
  4. 通过对UNIX系统调用的read()和write()进行读取。
  5. Doesn't include buffering and such features.
  6. 不包括缓冲和这些特性。
  7. Less portable and lacks efficiency.
  8. 便携性差,效率低。

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.

文件指针是fopen()库函数返回的C结构的指针,该函数用于标识文件、封装文件描述符、缓冲功能以及I/O操作所需的所有其他功能。文件指针是文件类型,其定义可以在“/usr/include/stdio.h”中找到。这个定义可能因编译器而异。

FILE *fp = fopen (filePath, mode);

// A FILE Structure returned by fopen 
    typedef struct 
    {
        unsigned char   *_ptr;
        int     _cnt;
        unsigned char   *_base;
        unsigned char   *_bufendp;
        short   _flag;
        short   _file;
        int     __stdioid;
        char    *__newbase;
#ifdef _THREAD_SAFE
        void *_lock;
#else
        long    _unused[1];
#endif
#ifdef __64BIT__
        long    _unused1[4];
#endif /* __64BIT__ */
    } FILE;
  1. It is high level interface.
  2. 它是高级界面。
  3. Passed to fread() and fwrite() functions.
  4. 传递给fread()和fwrite()函数。
  5. Includes buffering,error indication and EOF detection,etc.
  6. 包括缓冲、错误指示和EOF检测等。
  7. Provides higher portability and efficiency.
  8. 提供更高的可移植性和效率。

#6


3  

FILE * is more useful when you work with text files and user input/output, because it allows you to use API functions like sprintf(), sscanf(), fgets(), feof() etc.

在处理文本文件和用户输入/输出时,FILE *更有用,因为它允许您使用sprintf()、sscanf()、fgets()、feof()等API函数。

File descriptor API is low-level, so it allows to work with sockets, pipes, memory-mapped files (and regular files, of course).

文件描述符API是低级的,因此它允许使用套接字、管道、内存映射文件(当然还有常规文件)。

#7


2  

Just a note to finish out the discussion (if interested)....

只是完成报告讨论(如果感兴趣)....

fopen can be insecure, and you should probably use fopen_s or open with exclusive bits set. C1X is offering x modes, so you can fopen with modes "rx", "wx", etc.

fopen可能是不安全的,您可能应该使用fopen_s或带有独占位集的open。C1X提供x模式,因此可以使用“rx”、“wx”等模式进行fopen。

If you use open, you might consider open(..., O_EXCL | O_RDONLY,... ) or open(..., O_CREAT | O_EXCL | O_WRONLY,... ).

如果你使用open,你可以考虑open(……))。或者打开(…)。

See, for example, Do not make assumptions about fopen() and file creation.

例如,不要假设fopen()和文件创建。

#8


1  

System calls are mostly using file descriptor, for example read and write. Library function will use the file pointers ( printf , scanf). But, library functions are using internally system calls only.

系统调用主要使用文件描述符,例如读和写。库函数将使用文件指针(printf, scanf)。但是,库函数只使用内部系统调用。

#9


1  

In Unix and related computer operating systems, a file descriptor (FD, less frequently fildes) is an abstract indicator (handle) used to access a file or other input/output resource, such as a pipe or network socket. File descriptors form part of the POSIX application programming interface. A file descriptor is a non-negative integer, although, it is usually represented in C programming language as the type int, negative values being reserved to indicate "no value" or an error condition.

在Unix和相关的计算机操作系统中,文件描述符(FD,不太频繁的文件)是用于访问文件或其他输入/输出资源(如管道或网络套接字)的抽象指示符(句柄)。文件描述符是POSIX应用程序编程接口的一部分。文件描述符是一个非负整数,但是,它通常在C编程语言中表示为类型int,为表示“无值”或错误条件而保留的值为负值。

#1


113  

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.

文件描述符是一个低级整数“句柄”,用于在Linux和其他类unix系统的内核级标识打开的文件(或套接字或其他)。

You pass "naked" file descriptors to actual Unix calls, such as read(), write() and so on.

将“裸”文件描述符传递给实际的Unix调用,例如read()、write()等等。

A FILE pointer is a C standard library-level construct, used to represent a file. The FILE wraps the file descriptor, and adds buffering and other features to make I/O easier.

文件指针是C标准库级结构,用于表示文件。该文件包装文件描述符,并添加缓冲和其他特性以使I/O更容易。

You pass FILE pointers to standard C functions such as fread() and fwrite().

将文件指针传递给标准的C函数,如fread()和fwrite()。

#2


49  

One is buffered (FILE *) and the other is not. In practice, you want to use FILE * 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..

一个是缓冲(文件*),另一个不是。在实践中,当您从一个“真实”文件(例如)中读取文件时,您几乎总是希望使用FILE *。在驱动器上),除非你知道你在做什么或除非你的文件实际上是一个套接字左右。

You can get the file descriptor from the FILE * using fileno() and you can open a buffered FILE * from a file descriptor using fdopen()

可以使用fileno()从文件*获得文件描述符,也可以使用fdopen()从文件描述符*打开缓冲文件*

#3


13  

A file descriptor is just an integer which you get from the Posix' open()call. Using the standard C fopen() you get a FILE struct back. The FILE struct contains the this file descriptor amongst other things such as end-of-file and error indicator, stream position etc.

文件描述符只是从Posix' open()调用中获得的一个整数。使用标准的C fopen()可以得到一个文件结构。文件结构体包含这个文件描述符以及诸如文件结束、错误指示符、流位置等等。

So using fopen() gives you a certain amount of abstraction compared to open(). In general you should be using fopen() since that is more portable and you can use all the other standard C functions that uses the FILE struct, ie fprintf() and family.

因此,与open()相比,使用fopen()可以提供一定的抽象。一般来说,您应该使用fopen(),因为它更便于携带,您可以使用所有使用文件结构的标准C函数,即fprintf()和家庭。

There are no performance issues using either or.

使用or时没有性能问题。

#4


9  

Want to add points which might be useful.

想要添加一些有用的点。

ABOUT FILE *

关于文件*

  1. can't be used for interprocess communication(IPC).
  2. 不能用于进程间通信(IPC)。
  3. use it when you need genral purpose buffered I/O.(printf,frpintf,snprintf,scanf)
  4. 当您需要genral目的缓冲I/ o时使用它(printf、frpintf、snprintf、scanf)
  5. I use it many times for debug logs. example,

    我多次使用它来调试日志。的例子,

                 FILE *fp;
                 fp = fopen("debug.txt","a");
                 fprintf(fp,"I have reached till this point");
                 fclose(fp);
    

ABOUT FILE DESCRIPTOR

关于文件描述符

  1. It's generally used for IPC.

    它通常用于IPC。

  2. Gives low-level control to files on *nix systems.(devices,files,sockets,etc), hence more powerfull than the FILE *.

    对*nix系统上的文件进行低级控制(设备、文件、套接字等),因此比文件*更强大。

#5


7  

File descriptor vs File pointer

文件描述符vs文件指针。

File descriptor:

文件描述符:

File Descriptor is an integer value returned by open() system call.

文件描述符是open()系统调用返回的整数值。

int fd = open (filePath, mode);

int fd = open (filePath, mode);

  1. Low/Kernel level handler.
  2. 低/内核级别的处理程序。
  3. passe to read() and write() of UNIX System Calls.
  4. 通过对UNIX系统调用的read()和write()进行读取。
  5. Doesn't include buffering and such features.
  6. 不包括缓冲和这些特性。
  7. Less portable and lacks efficiency.
  8. 便携性差,效率低。

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.

文件指针是fopen()库函数返回的C结构的指针,该函数用于标识文件、封装文件描述符、缓冲功能以及I/O操作所需的所有其他功能。文件指针是文件类型,其定义可以在“/usr/include/stdio.h”中找到。这个定义可能因编译器而异。

FILE *fp = fopen (filePath, mode);

// A FILE Structure returned by fopen 
    typedef struct 
    {
        unsigned char   *_ptr;
        int     _cnt;
        unsigned char   *_base;
        unsigned char   *_bufendp;
        short   _flag;
        short   _file;
        int     __stdioid;
        char    *__newbase;
#ifdef _THREAD_SAFE
        void *_lock;
#else
        long    _unused[1];
#endif
#ifdef __64BIT__
        long    _unused1[4];
#endif /* __64BIT__ */
    } FILE;
  1. It is high level interface.
  2. 它是高级界面。
  3. Passed to fread() and fwrite() functions.
  4. 传递给fread()和fwrite()函数。
  5. Includes buffering,error indication and EOF detection,etc.
  6. 包括缓冲、错误指示和EOF检测等。
  7. Provides higher portability and efficiency.
  8. 提供更高的可移植性和效率。

#6


3  

FILE * is more useful when you work with text files and user input/output, because it allows you to use API functions like sprintf(), sscanf(), fgets(), feof() etc.

在处理文本文件和用户输入/输出时,FILE *更有用,因为它允许您使用sprintf()、sscanf()、fgets()、feof()等API函数。

File descriptor API is low-level, so it allows to work with sockets, pipes, memory-mapped files (and regular files, of course).

文件描述符API是低级的,因此它允许使用套接字、管道、内存映射文件(当然还有常规文件)。

#7


2  

Just a note to finish out the discussion (if interested)....

只是完成报告讨论(如果感兴趣)....

fopen can be insecure, and you should probably use fopen_s or open with exclusive bits set. C1X is offering x modes, so you can fopen with modes "rx", "wx", etc.

fopen可能是不安全的,您可能应该使用fopen_s或带有独占位集的open。C1X提供x模式,因此可以使用“rx”、“wx”等模式进行fopen。

If you use open, you might consider open(..., O_EXCL | O_RDONLY,... ) or open(..., O_CREAT | O_EXCL | O_WRONLY,... ).

如果你使用open,你可以考虑open(……))。或者打开(…)。

See, for example, Do not make assumptions about fopen() and file creation.

例如,不要假设fopen()和文件创建。

#8


1  

System calls are mostly using file descriptor, for example read and write. Library function will use the file pointers ( printf , scanf). But, library functions are using internally system calls only.

系统调用主要使用文件描述符,例如读和写。库函数将使用文件指针(printf, scanf)。但是,库函数只使用内部系统调用。

#9


1  

In Unix and related computer operating systems, a file descriptor (FD, less frequently fildes) is an abstract indicator (handle) used to access a file or other input/output resource, such as a pipe or network socket. File descriptors form part of the POSIX application programming interface. A file descriptor is a non-negative integer, although, it is usually represented in C programming language as the type int, negative values being reserved to indicate "no value" or an error condition.

在Unix和相关的计算机操作系统中,文件描述符(FD,不太频繁的文件)是用于访问文件或其他输入/输出资源(如管道或网络套接字)的抽象指示符(句柄)。文件描述符是POSIX应用程序编程接口的一部分。文件描述符是一个非负整数,但是,它通常在C编程语言中表示为类型int,为表示“无值”或错误条件而保留的值为负值。