您的位置:首页 > 运维架构 > Linux

linux 实现cat命令

2015-09-13 16:39 417 查看
#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;

}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: