问题
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?
回答1:
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. The FILE
wraps the file descriptor, and adds buffering and other features to make I/O easier.
You pass FILE
pointers to standard C functions such as fread()
and fwrite()
.
回答2:
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..
You can get the file descriptor from the FILE *
using fileno()
and you can open a buffered FILE *
from a file descriptor using fdopen()
回答3:
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.
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.
There are no performance issues using either or.
回答4:
File descriptor vs File pointer
File descriptor:
File Descriptor is an integer value returned by open()
system call.
int fd = open (filePath, mode);
- Low/Kernel level handler.
- passe to read() and write() of UNIX System Calls.
- Doesn't include buffering and such features.
- Less portable and lacks efficiency.
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.
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;
- It is high level interface.
- Passed to fread() and fwrite() functions.
- Includes buffering,error indication and EOF detection,etc.
- Provides higher portability and efficiency.
回答5:
Want to add points which might be useful.
ABOUT FILE *
- can't be used for interprocess communication(IPC).
- use it when you need genral purpose buffered I/O.(printf,frpintf,snprintf,scanf)
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
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 *
.
回答6:
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 descriptor API is low-level, so it allows to work with sockets, pipes, memory-mapped files (and regular files, of course).
回答7:
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.
If you use open
, you might consider open(..., O_EXCL | O_RDONLY,... )
or open(..., O_CREAT | O_EXCL | O_WRONLY,... )
.
See, for example, Do not make assumptions about fopen() and file creation.
回答8:
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.
来源:https://stackoverflow.com/questions/2423628/whats-the-difference-between-a-file-descriptor-and-file-pointer