Convert file descriptor to file pointer | Convert file pointer to file descriptor in Linux by C programming | Share Programming Tips

RSS Feeds

- Home
- About Us
- Contact Us
- Share Your Project

Jan
30
1

# Convert file descriptor to file pointer | Convert file pointer to file descriptor in Linux by C programming

JIGAR PATEL c, linux, Tips, tips fdopen(), fileno()

If you all ready know the difference between file descriptor and file pointer then good otherwise please read this post

# Whats is difference between file descriptor and file pointer?

## File descriptor to file pointer :

for converting file descriptor to file pointer in linux fdopen() function is used

    #include <stdio.h>

    FILE *fdopen(int fildes, const char *mode);

fildes is file descriptor

mode is the character value which indicate the mode of file operation (same as we use in fopen() function)

Example :

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

    int main(){
    int fd;
    FILE *fp;
    fd = open("shareprogrammingtips.txt",O_WRONLY | O_CREAT | O_TRUNC);
    if(fd<0)
    {
       printf("open call fail");
       return -1;
    }

    fp=fdopen(fd,"w");
    fprintf(fp,"we got file pointer fp bu using File descriptor fd");
    fclose(fp);
    close(fd);
    return 0;
    }

## File pointer to file descriptor:

for converting file pointer to file descriptor in linux fdopen() function is used

`#include <stdio.h>`

``

`int fileno(FILE *`stream`);`

stream is file pointer

Example:

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>

    int main(){
    int fd;
    FILE *fp;
    int size;
    char *buffer;
    fp = fopen("shareprogrammingtips.txt","r");
    if(fp==NULL)
    {
       printf("open call fail");
       return -1;
    }
    /* Logic for calculating size of file*/
    fseek(fp,0,SEEK_END);
    size = ftell(fp);
    fseek(fp,0,0);

    if((buffer=malloc(size)) == NULL)
    {
    printf("Malloc fail");
    return -1;
    }

    fd=fileno(fp);
    read(fd, (void*)buffer, size);
    printf("size is %d Buffer is %s ",size,buffer);
    fclose(fp);
    close(fd);
    return 0;
    }

- Array
- Array
- Array
- Array
- Array
- Array
- Array

### JIGAR PATEL

hey I am an Artist who love to write code…! Well I am an EC graduate From Ganpat University and now i am working as Embedded software engineer in one private firm.. find me at here JigAr Patel

More Posts – Website – Twitter – Facebook

### You may like to read this also....

- How To Root Your HTC Incredible S
  The HTC Incredible S is a beast, but it’s more like one that’s chained and caged. To r...
- Backend Service Provider StackMob Comes To Android
  StackMob, the backend service provider for mobile developers, has big news today: it’s now available...
- Most common directory layout seen in all C/C++ based Project/Package/Library
  I have been with lots of projects package or library written in C or C++ programming language has On...
- Top Selling Best Android phones in India.
  1) Samsung Galaxy S II (I9100) Specifi...
- Whether should i use processes or threads in Linux?
  While designing any system usually we might confuse between thread and process usage . We think ...

If you enjoyed this article please consider sharing it!

## 1 Comment to “Convert file descriptor to file pointer | Convert file pointer to file descriptor in Linux by C programming”

- Whats is difference between file descriptor and file pointer, file descriptor and file pointer, Share Programming Tips January 30, 2012 at 5:12 pm

  [...] Convert file descriptor to file pointer | Convert file pointer to file descriptor in Linux by C prog… JIGAR PATELhey I am an Artist who love to write code…! Well I am an EC graduate From Ganpat University and now i am working as Embedded software engineer in one private firm.. find me at here GoogleWebsite – Twitter – Facebook – More Posts You may like to read this also….Embedded Jobs and Careers | embeddedcareers.comHey friends, after having some experience in embedded field i always get to know about recent trades…Concept of Octal literal in COne day while debugging one code i got one interesting things ..let me explain you by bellow's exa…Full Specifications of Leaked Samsung Galaxy S3Samsung is planning to launch 3D Android smartphones next year. Company is actively working on the n…Implementation limitation of c programming language One day i thought , "What is the maximum number of arguments we can pass to a function in C ?" …How to set, clear ,toggle and check a single bit in C?As being an embedded software engineer i would really suggest you this tricks. ..! In many applica… If you enjoyed this article please consider sharing it! [...]

  Reply

## Post comment

Click here to cancel reply.

Name (required)

Mail (will not be published) (required)

Website

Comment

CAPTCHA Code *

Follow us on Twitter!

## Search in this website

## our sponsors

## latest comments

- Shah Dhaval on How to detect memory leakage in C program?
- ruunyytikkil on how can I write applications in C or C++ for Android?
- anonym on How to convert SQL to Excel in Php?
- Academic Projects and Tech interview on C programming interview questions and answers for freshers
- Motasim billah on List of all standard version of c language

## Find us on Facebook

## Top Authors

JIGAR PATEL
35 posts
saurabh
21 posts
Arpi
10 posts

## Find us on stackoverflow

## Polls

Tell us who you are

- Web Developer
- Web Designer
- Software engineer
- Embedded engineer
- Any other Engineer
- i am just regular user

View Results

Loading ...

- Polls Archive

## My Bookmarks

- Audio/video Recorder & player application based on MATLAB
- check dependency of your binary
- defination of all standard c programming language function
- Great Question-Answer on c programming
- know what your c code means
- Limition of c programming language
- List of all version of c programming language
- Online c compiler
- php freelancing work
- some more stuff on C programming language
- Volatile Keyword in Embedded System
- Write Android application in c language

## Sponsers Link