linux 实现cat命令

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>

int main(int argc,char **argv)
{
 int fd;
 char buf[64];
 int ret;

 fd=open(argv[1],O_RDONLY);  /*以只读的方式打开文件*/
 if(fd<0)
 {
  perror("open()");
  return -1;
 }
 while(1){
  bzero(buf,64);
  ret=read(fd,buf,64);    /*从文件中读数据,*/
  if(ret<=0)
  {
   break;
  }
  write(1,buf,ret);      /*打印出来*/
 }
 close(fd);
 return 0;
}

猜你喜欢

转载自blog.csdn.net/d1306937299/article/details/48416583
今日推荐