ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
/*
int descr for my problem it was a fifo O_RDONLY descriptor
*/
int test(int descr){
char buff[1024]={0};
FILE *fp=fdopen(descr,"r");
fgets(buff,1023,fp); // P1
int len=read(descr,buff,1023); // P2
return len;
}
P2 will return 0
if i replace P2 with fread(buff,sizeof(char),1023,fp); it works
adding the line
descr=fileno(fp); between P1 and P2 doesn't work too (btw: descr == fileno(fp))
Q1: why can't i use the descriptor if i did a fdopen?
Q2: is it possible to use the descriptor after using fdopen? if yes, how?
Q2.2: is it possible to mix the usage of descriptor and file pointer?
if i read the article i think that the only difference between FILE pointer and descriptor is their different "file position" handling
file pointer and descriptor use different offsets
right?
if yes, why read(); returns 0 in the above example and doesn't return the same as fread returns?
Try using setbuf(fp, NULL) right after fopen() or fdopen(). You need to turn off any buffering and I guess that fread() is filling one buffer and making read() return 0 (EOF), in the case your file is large enough...
@primo
damn, youre absolutly right
using this code
Code:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,char **argv){
FILE *fp=fopen("readtest.cpp","r");
setbuf(fp,NULL);
int descr=fileno(fp);
char buffer[100]={0};
fread(buffer,sizeof(char),99,fp);
printf("%s",buffer);
memset(buffer,0,100);
int i=read(descr,buffer,99);
printf("\nREAD: %d %s\n",i,buffer);
exit(0);
}
it worked perfectly and i only added setbuf(fp,NULL);
just for understanding:
fread seems to buffer the input data
the problem was that my readtest.cpp was too small in size, so it was read completely by the fread function and the read thought it was at the end of the file
using a bigger file "read" continues reading where the buffer of fread ends (it's not necessaraliy the fread buffer parameter)
to mix both fread and read you have to set the file pointer fp to be unbuffered using as primo said
setbuf(fp,NULL);
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.