一起talk C栗子吧(第七十六回:C语言实例--DIY cat命令)

本文详细介绍了如何在Linux系统中DIY一个cat命令,包括操作步骤、代码实现及与系统cat命令的对比,旨在提供一个简单的命令行工具实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


各位看官们,大家好,上一回中咱们说的是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命令的例子咱们就说到这里。欲知后面还有什么例子,且听下回分解。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

talk_8

真诚赞赏,手有余香

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值