Jan
30

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

 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;
}
 

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 PostsWebsiteTwitterFacebook

You may like to read this also....

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! [...]

Post comment

*

CommentLuv badge
Follow us on Twitter! Follow us on Twitter!

Search in this website

our sponsors

latest comments

Find us on Facebook

Top Authors

35 posts
saurabh
21 posts
9 posts

Find us on stackoverflow

Polls

Tell us who you are

View Results

Loading ... Loading ...

My Bookmarks

Sponsers Link