问题
I have connected a modem to my pc(linux) through serial port and i am trying to write to and read from modem using write and read system call.During reading read system call blocks after reading all data from modem,in my code i have made check for eof as shown below
while(1)
{
if((rd_cnt =read(fd, &str, 1)) <=0)
{
perror("Read to dev failed");
printf("error no=%d\n",errno);
break;
}
printf("char =%c -> hex=%x rd_cnt=%d\n",str,str,rd_cnt);
sleep(3);
str=0;
}
if(rd_cnt==0)
printf("EOF met\n");
but i have never got eof,i wanted to read all the data send from modem till i meet end of file. Please guide me.
回答1:
change your code like this
if((rd_cnt =read(fd, &str, sizeof(str))) <=0)
{
perror("Read to dev failed");
printf("error no=%d\n",errno);
break;
}
That is ok to check the EOF
condition.
No need to write this code again as you are already checking this.
//if(rd_cnt==0)
//printf("EOF met\n");
来源:https://stackoverflow.com/questions/18399812/read-system-call-not-detecting-end-of-file