美文网首页
read-write-open(O_RDONLY) figer

read-write-open(O_RDONLY) figer

作者: 半步江南 | 来源:发表于2018-12-22 22:22 被阅读14次
    #include <stdlib.h>
    #include <fcntl.h>
    #include <stdio.h>
    
    int main(){
    
      char buffer[11];
      int nread=1;
      int fd;
      char * path;
      int i,flag;
      char path1[]="/root/study-dir/linux_program_design/chapter_3/read.c";
      fd = open(path1,O_RDONLY);
      while(nread){
      nread = read(fd,buffer,10);
      i=0,flag=0;
      while(i<10){
         printf("%d\n",buffer[i]);
        if(buffer[i]==10){
          flag=-1;
          break;
        }
    
        i++;
       }
      if(flag!=-1)
        {
           buffer[10]='\n';
           if (nread == -1)
        write(2,"A read error has occurred\n",26);
    
      if ((write(1,buffer,nread+1)) != nread+1)
        write(2,"A write error has occurred\n",27);
      printf("%d\n",nread);
        }else{
        if (nread == -1)
        write(2,"A read error has occurred\n",26);
    
      if ((write(1,buffer,nread)) != nread)
        write(2,"A write error has occurred\n",27);
      printf("%d\n",nread);
      }
    
    
      }
    
      exit(0);
    }
    

    相关文章

      网友评论

          本文标题:read-write-open(O_RDONLY) figer

          本文链接:https://www.haomeiwen.com/subject/qvophqtx.html