Read system call not detecting End of file

寵の児 提交于 2019-12-25 10:56:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!