What is difference between file descriptor and file pointer? [duplicate]
Asked Answered
R

3

5

Possible Duplicate:
What's the difference between a file descriptor and file pointer?

If I open file like this:

FILE *fp = fopen("mr32.txr","r");

then fp is file pointer or file descriptor? What is difference between them?

Romaine answered 19/11, 2011 at 4:28 Comment(0)
N
4

fp is a FILE pointer

File pointer:

  1. It is high level interface
  2. Passed to fread() and fwrite() functions
  3. Includes buffering,error indication and EOF detection,etc.
  4. Provides higher portability and efficiency.

File descriptor:

  1. Low/Kernel level handler
  2. passe to read() and write() of UNIX System Calls
  3. Doesn't include buffering and such features
  4. Less portable and lacks efficiency

based on this link

Nimmons answered 19/11, 2011 at 4:41 Comment(0)
D
4

It's a pointer to a FILE structure, if that's what you're asking. A file descriptor is an integer. The FILE structure and its related APIs are part of the C standard. File descriptors and their related functions are not. In practice you can use either set of functions interchangeably, though there are some notable differences in default behaviour here and there. You can read the man pages to figure out which functions take which sort of parameters. On systems that have file descriptors, you can usually use the fdopen(3) function to get a FILE structure from an open file descriptor and fileno(3) to go the other way.

Dendro answered 19/11, 2011 at 4:31 Comment(2)
It's worth noting that FILE is an opaque pointer (the memory it points to is meaningless to the code using the interface.) It's also worth noting that POSIX open returns a file descriptor.Judicial
And worth noting that fdopen creates a new FILE object (which needs to be closed, and upon closing which the file descriptor is also closed), while fileno simply returns the existing file descriptor underlying a FILE.Hussite
N
4

fp is a FILE pointer

File pointer:

  1. It is high level interface
  2. Passed to fread() and fwrite() functions
  3. Includes buffering,error indication and EOF detection,etc.
  4. Provides higher portability and efficiency.

File descriptor:

  1. Low/Kernel level handler
  2. passe to read() and write() of UNIX System Calls
  3. Doesn't include buffering and such features
  4. Less portable and lacks efficiency

based on this link

Nimmons answered 19/11, 2011 at 4:41 Comment(0)
E
2

FILE is a struct that contains information about the file, including the file descriptor.

Eloiseelon answered 19/11, 2011 at 4:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.