#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);
}
网友评论