您的位置:首页 > 编程语言 > C语言/C++

一起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的代码,请大家参考:

/* **************************
* 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命令的例子咱们就说到这里。欲知后面还有什么例子,且听下回分解。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: