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:
Code:
int fileDescriptor = open ("/test.txt", O_APPEND);


For file pointer:
Code:
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;
#ifdef _THREAD_SAFE
        void *_lock;
#else
        long    _unused[1];
#endif
#ifdef __64BIT__
        long    _unused1[4];
#endif /* __64BIT__ */
} FILE;

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.
Code:

#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);
    close(fileNumber);

    return 0;
}



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

    fclose (filePointer);
    close(fileNumber);


You can get the file descriptor from file descriptor.



Re: file descriptor vs file pointer

Thu Feb 07, 2013 8:51 am

Hi,
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     -  
 Reading the all file in php     -  
 read from file in C++     -  
 I am new in PHP, want coding or PHP file     -  

Topic Tags

C++ Files and I/O