Hi all, I want to know the difference between the file descriptor and file pointer.And also which is efficient in scenarios.
What is difference between file descriptor and file pointer - C
http://www.daniweb.com/forums/thread98191.html
file descriptor and file pointer [Archive] -
http://answers.yahoo.com/question/index?qid=20090209003227AAEQsI4
what's difference between fd and fp?
http://cboard.cprogramming.com/c-programming/90357-whats-difference-between-fd-fp.html
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()
.
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.
System calls are mostly using file descriptor. for example ( read , write ) .
Library function will use the file pointers ( printf , scanf ) ;
But , library function are using internally system calls only.
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).
One is buffered (FILE *
) and the other is not. In preactice, 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()