Total members 11890 |It is currently Wed Apr 24, 2024 6:00 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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.



_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

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


Author:
Newbie
User avatar Posts: 2
Have thanks: 0 time
Post new topic Reply to topic  [ 2 posts ] 

  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     -  
 Delete file     -  
 Random to File     -  
 Close a file in php     -  



Topic Tags

C++ Files and I/O






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com