views:

578

answers:

6

Hi all, I want to know the difference between the file descriptor and file pointer.And also which is efficient in scenarios.

+1  A: 

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

ratty
I think karthi is talking about `FILE *` type.
Ben
+4  A: 

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().

unwind
@nvl: fildes is surely available to Windows, e.g. http://msdn.microsoft.com/en-us/library/z0kc8e3z%28VS.80%29.aspx
KennyTM
@kennyTM: thanks for correcting :).
N 1.1
+1  A: 

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.

Martin Wickman
+1 for bringing up portability. FILE is part of the Standard C Library (back to C89/C90); file descriptors are not.
tomlogic
+1  A: 

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.

pavun_cool
A: 

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).

qrdl
+1  A: 

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()

Ben
+1 for pointing out fileno(), the organization of the man pages makes this one tough to find. Same for fdopen().
BD at Rivenhill