一起talk C栗子吧(第七十六回:C语言实例--DIY cat命令)
2015-12-16 22:46
549 查看
各位看官们,大家好,上一回中咱们说的是DIY cd命令的例子,这一回咱们说的例子是:DIY cat命令。闲话休提,言归正转。让我们一起talk C栗子吧!
看官们,在Linux系统中,我们经常使用cat命令来查看一些小文件的内容。因此,它也是我们经常使用的命令之一。今天,让我们一起来DIY cat命令吧。
下面是具体的操作步骤:
1.从用户输入的命令中获取文件名;
2.使用fopen函数打开该文件;
3.使用fgets函数从文件中读取内容,并且保存到一个数组中;
4.输出数组中的内容到终端中;
5.重复步骤3和4,直到文件中的所有内容都被读取为止;
6.使用fclose函数关闭文件;
下面是我们DIY的代码,请大家参考:
通过查看上面的代码,大家可以发现我们DIY的命令只支持“cat filename”这种命令格式。目前,还不支持cat命令中的其它参数,以后可以再加上,以便进一步完善DIY的cat命令。
看官们,正文中就不写代码了,详细的代码放到了我的资源中,大家可以点击这里下载使用。此外,该函数需要与前面章回中的main.c和main.h一起配合使用。
下面是程序的运行结果,请大家参考:
下面是系统中cat命令的运行结果:
看官们,通过对比上面的运行结果大家可以看到,DIY cat命令的结果和系统中cat命令的运行结果是完全一致的。
各位看官,关于DIY cat命令的例子咱们就说到这里。欲知后面还有什么例子,且听下回分解。
看官们,在Linux系统中,我们经常使用cat命令来查看一些小文件的内容。因此,它也是我们经常使用的命令之一。今天,让我们一起来DIY cat命令吧。
下面是具体的操作步骤:
1.从用户输入的命令中获取文件名;
2.使用fopen函数打开该文件;
3.使用fgets函数从文件中读取内容,并且保存到一个数组中;
4.输出数组中的内容到终端中;
5.重复步骤3和4,直到文件中的所有内容都被读取为止;
6.使用fclose函数关闭文件;
下面是我们DIY的代码,请大家参考:
/* ************************** * The cat command of shell * *************************/ #include "main.h" int cats(const char *p) { char path[PATH_SIZE]; char *start; char *end; char buf[PATH_SIZE]; FILE *fp; memset(path,'\0',PATH_SIZE); // must init ,ohtrway there is a wrong result with @ start = strchr(p,' '); end = strchr(p,'\n'); if(!start || !end) { printf("can't support this format \n"); return 1; } strncpy(path,p+4,end-start-1); // get the path in inputting command fp=fopen(path,"r"); // opne the file if(NULL==fp) { printf("can't opne file:%s \n",path); return 1; } while(NULL != fgets(buf,PATH_SIZE,fp)) // get the content of file ,and show them printf("%s",buf); fclose(fp); // close the file,after using it return 0; }
通过查看上面的代码,大家可以发现我们DIY的命令只支持“cat filename”这种命令格式。目前,还不支持cat命令中的其它参数,以后可以再加上,以便进一步完善DIY的cat命令。
看官们,正文中就不写代码了,详细的代码放到了我的资源中,大家可以点击这里下载使用。此外,该函数需要与前面章回中的main.c和main.h一起配合使用。
下面是程序的运行结果,请大家参考:
|->cat cat.c //执行diy的cat命令
/* ************************** * The cat command of shell * *************************/ #include "main.h" int cats(const char *p) { char path[PATH_SIZE]; char *start; char *end; char buf[PATH_SIZE]; FILE *fp; memset(path,'\0',PATH_SIZE); // must init ,ohtrway there is a wrong result with @ start = strchr(p,' '); end = strchr(p,'\n'); if(!start || !end) { printf("can't support this format \n"); return 1; } strncpy(path,p+4,end-start-1); // get the path in inputting command fp=fopen(path,"r"); // opne the file if(NULL==fp) { printf("can't opne file:%s \n",path); return 1; } while(NULL != fgets(buf,PATH_SIZE,fp)) // get the content of file ,and show them printf("%s",buf); fclose(fp); // close the file,after using it return 0; }
|->exit //退出diy的shell
下面是系统中cat命令的运行结果:
$ cat cat.c
/* ************************** * The cat command of shell * *************************/ #include "main.h" int cats(const char *p) { char path[PATH_SIZE]; char *start; char *end; char buf[PATH_SIZE]; FILE *fp; memset(path,'\0',PATH_SIZE); // must init ,ohtrway there is a wrong result with @ start = strchr(p,' '); end = strchr(p,'\n'); if(!start || !end) { printf("can't support this format \n"); return 1; } strncpy(path,p+4,end-start-1); // get the path in inputting command fp=fopen(path,"r"); // opne the file if(NULL==fp) { printf("can't opne file:%s \n",path); return 1; } while(NULL != fgets(buf,PATH_SIZE,fp)) // get the content of file ,and show them printf("%s",buf); fclose(fp); // close the file,after using it return 0; }
看官们,通过对比上面的运行结果大家可以看到,DIY cat命令的结果和系统中cat命令的运行结果是完全一致的。
各位看官,关于DIY cat命令的例子咱们就说到这里。欲知后面还有什么例子,且听下回分解。
相关文章推荐
- c++中utf-8转gb2312
- c语言中的运算优先级
- C++ 11: function,bind和lambda表达式
- C++中mutable关键的学习
- [笔记]c++包含纯虚函数的类不允许被实例化
- C语言趣味编程两小例
- [转][C++ 11]override and final - write clean and maintainable C++ code
- C语言学习笔记----伊能C语言学习笔记---*号的作用
- C /C++标准库 - <cstring> (string.h)
- C/C++:函数的编译方式与调用约定以及extern “C”的使用
- C语言学习笔记----伊能C语言学习笔记----指针与地址的区别是什么?
- C++中extern “C”含义深层探索
- 尾插法创建带头结点单链表(c语言版)
- 【翻译】std::list::remove - C++ Reference
- c++函数指针
- c或c++中int转float中在printf中的问题
- hdoj1002(A+B problem II 大数相加 c语言
- 实战c++中的vector系列--使用vector构造二维数组
- 实战c++中的vector系列--使用vector构造二维数组
- udp组播试验-C语言