此次实现的是cat的比较常用的查看文件的内容的功能。
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<unistd.h>
4 #include<fcntl.h>
5 #define BUFSIZE 4096
6
7 int main(int argc,char *argv[])
8 {
9 int fd,ret;
10 char buf[BUFSIZE];
11 char s[BUFSIZE];
12 if(argc<2)
13 {
14 printf(“argument not enough”);
15 return -1;
16 }
17
18 if((fd=open(argv[1],O_RDWR|O_CREAT))-1)
19 {
20 perror(“OPEN ERROR”);
21 return -1;
22 }
23 if(ret=(read(fd,buf,BUFSIZE))-1)
24 {
25 perror(“read error”);
26 return -1;
27 }
28 printf("%s",buf);
29 close(fd);
30 return 0;
31 }
32
使用已学函数实现cat命令的功能
最新推荐文章于 2022-03-06 22:38:46 发布