what's difference between fd and fp?

This is a discussion on what's difference between fd and fp? within the C Programming forums, part of the General Programming Boards category; Hi. Just wondering what the main differences between a file pointer and a file descriptor are. I know that a ...

  1. #1
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72

    what's difference between fd and fp?

    Hi. Just wondering what the main differences between a file pointer and a file
    descriptor are.

    I know that a the FILE struct keeps info on the file stream in use. Correct?
    But doesn't a file descriptor do similar?

    I mean, if the read() reads 1024 bytes from fd, and then does another call to the
    same descriptor, it will begin in the file where the last read() left off. Similar to
    how FILE *fp works?

    Code:
    int fd;
    
    nread = read(fd, buf, 1024);

    thanks
    Remember that all that code you write turns into this:

    0100100100110010010011100100111001001
    0010100100100001001111100010010010010 ....

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    The whole FILE system wraps the file system that uses file descriptors.

    One of the fields in struct FILE will be a variable to hold the file descriptor of the file it opened.

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    A FILE object is a standard C entity. Wherever C exists, you can use FILE objects. A file descriptor, on the other hand, is a UNIX-specific concept and has nothing to do with standard C.

    The C standard I/O library conceals the platform-specific details of file access. A file descriptor is the total opposite -- the most platform-specific thing you can get.

  4. #4
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    I'm not sure that is entirely correct. WIth access to stdio.h and hence the FILE structure, you also gain access to fileno() which will return a file descripter given a stream. So I'm pretty sure file descriptors exist as part of standard C (of course, their meaning/representation in the underlying file subsystem can vary depending on platform).

    Edit: nm, you are correct, I double checked IEEE standards and it indeed tags fileno() as an extension to ISO C.
    Last edited by @nthony; 05-30-2007 at 10:36 PM.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by @nthony View Post
    I'm not sure that is entirely correct. WIth access to stdio.h and hence the FILE structure, you also gain access to fileno() which will return a file descripter given a stream. So I'm pretty sure file descriptors exist as part of standard C (of course, their meaning/representation in the underlying file subsystem can vary depending on platform).
    fileno() isn't a standard function.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    As brewbuck was saying, file descriptors are the Unix way of handling files.

    For Windows, instead of a file descriptor, you receive a variable of a windows-specific type called a HANDLE, and you use that instead of a file descriptor.

    The C standard library is written on Windows to use the Windows specific file handling routines. On Unix, it's written to use the Unix specific file handling routines. Even something as simple as printf() has to call specific functions to put its data to the console output. In Unix, it's write(). For Windows, it's WriteConsole() (or alternatively WriteFile(), I believe). Everything is O/S specific when it comes to input and output, and especially when dealing with files.

    This is why you should use the standard C library when possible if you wish your code to compile on almost any given O/S. The benefit of this is that you don't have to be concerned with file descriptors or HANDLEs to files, and you especially don't have to worry about writing multiple different versions for every program to get them to compile on every O/S you wish to support

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    134

    Exclamation

    Hi Guys,

    when we say file pointer then it means a pointer variable of FILE type.
    which stores return value of "FILE *fopen(const char *path, const char *mode);"
    And when we say file descriptor then it means a int type variable which will store return value of "int open(const char *pathname, int flags);"

    Correct me if i m wromg.
    S_ccess is waiting for u. Go Ahead, put u there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A task in Linux. Can't make it work
    By sanchopansa in forum C Programming
    Replies: 6
    Last Post: 11-02-2006, 10:04 AM
  2. fd copy
    By mary in forum C Programming
    Replies: 12
    Last Post: 07-18-2002, 06:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21