Switch to full style
C/C++ Technology Tutorials Written By Members.
Post a reply

file descriptor vs file pointer

Mon Aug 27, 2012 9:58 pm

You may have a question what is the difference between file descriptor and file pointer when dealing with files I/O in C++ programming language, file descriptor is a integer positive number assigned to identify each file opened by a process in Linux operating system, while file pointer is a structure of type File that includes many file operations and information including the file descriptor.

For file descriptor number:
int fileDescriptor = open ("/test.txt", O_APPEND);

For file pointer:
FILE *filePointer = fopen ("/test.txt", "w");
File structure
typedef struct {
        unsigned char   *_ptr;
        int     _cnt;
        unsigned char   *_base;
        unsigned char   *_bufendp;
        short   _flag;
        short   _file;
        int     __stdioid;
        char    *__newbase;
        void *_lock;
        long    _unused[1];
#ifdef __64BIT__
        long    _unused1[4];
#endif /* __64BIT__ */

We can describe the differences between files’ pointer and files’ descriptor
- File Pointer is a structure with many I/O functionality EOF, and buffering.
- File Descriptor is a low level kernel variable for Linux operating system.

During C programming, file operations are pretty common. And when you are writing programs in C on Linux, two familiar terms related to file operations are File Pointers and File Descriptors.

#include <stdio.h>
#include <fcntl.h>

int main()
    int fileNumber;
    FILE *filePointer;
    fileNumber = open ("myfile.txt", O_WRONLY | O_CREAT);
    if (fileNumber < 0)
        printf("Can’t read file using file\n");
        exit (1);
    write (fileNumber, "Writing using File Number\n", 17);

    filePointer = fdopen(fileNumber, "a");
    fprintf(filePointer, "Writing using File Pointer\n");
    fclose (filePointer);

    return 0;

As you can see in the previous example we have get the file pointer from file descriptor, for vice versa :
    fileNumber = fileno (filePointer);
    write (fileNumber, "Writing using file descriptor \n", 17);

    fclose (filePointer);

You can get the file descriptor from file descriptor.

Re: file descriptor vs file pointer

Thu Feb 07, 2013 8:51 am

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

Post a reply
  Related Posts  to : file descriptor vs file pointer
 Encrypt/Decrypt a file from source file to target file.     -  
 Copy file to file in java code- implementation     -  
 getting file name of html input file tag using jsp     -  
 How to convert xml file to Pdf file using C     -  
 Change the mouse cursor to hand pointer     -  
 C++ File I/O     -  
 web.xml file     -  
 JSP to Download File     -  
 explode CSV file     -  
 Append to file using php     -  

Topic Tags

C++ Files and I/O