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; }
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”
Post comment
Search in this website
our sponsors
latest comments
- c programme language on Implementation limitation of c programming language
- codebreaker on How to access/unblock songs.pk in india?
- Esta on Free android applications
- PCB Design on Embedded Jobs and Careers | embeddedcareers.com
- JIGAR PATEL on List of all standard version of c language
Find us on Facebook
Top Authors
Find us on stackoverflow
Polls
Loading ...
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
[...] 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! [...]