1 1 #include<stdio.h> 2 2 #include<stdlib.h> 3 3 #include<unistd.h> 4 4 #include<string.h> 5 5 #include<errno.h> 6 6 #include<sys/types.h> 7 7 #include<sys/stat.h> 8 8 #include<fcntl.h> 9 9 10 10 int main(int arg,char *args[]) 11 11 { 12 12 13 13 if(arg<2) 14 14 return 0; 15 15 int fd = open(args[1],O_RDONLY); //fd文件描述符 16 16 if(fd==-1) 17 17 { 18 18 printf("error is %s ",strerror(errno)); 19 19 }else 20 20 { 21 21 printf("success fd=%d ",fd); 22 22 char buf[100]; 23 23 memset(buf,0,sizeof(buf)); 24 24 25 25 while(read(fd,buf,sizeof(buf)-1)>0) 26 26 { 27 27 printf("%s ",buf); 28 28 memset(buf,0,sizeof(buf));//循环读取文件内容,直达文件结尾,退出循环 29 29 } 30 30 31 31 close(fd); 32 32 } 33 33 return 0; 34 34 } 35