C语言 fgets妙用《导出Linux系统 man 2 指定函数说明文本》

需求:将Linux里的man 2 open 或其他函数,生成txt文件放入指定目录;

思路:C语言模拟输入man 2 open > /mnt/hgfs/xxxopen.txt 实现需求;

运用函数:   

    //获取含有空格的键盘输入内容;(注意fgets自动添加的'\n',见如下图解)
    fgets(com2,50,stdin);   

    // delect fgets give byte:'\n'; 将'\n'替换为'\0';
    com2[(strlen(com2)-1)] = '\0';

运行:

 查看生成文件:

脚本源码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
    char command[100];
    char com2[50];

    memset(com2,'\0',50);

    memset(command,'\0',100);
    // 设置要执行的命令,这里以 "man 2 open" 为例
    printf("please input command (demo: man 2 open)\n");
    fgets(command,100,stdin);
    // delect fget give byte:'\n';
    command[(strlen(command)-1)] = '>';
    command[(strlen(command)-0)] = '\0';

    printf("please input target derectory (demo:/mnt/hgfs/osFire/man_2_open.txt)\n");
    fgets(com2,50,stdin);
    // delect fget give byte:'\n';
    comm2[(strlen(com2)-1)] = '\0';

    //pingjie 
    strcat(command,com2);  // 将输出重定向到文件
    system(command);

    // 输出成功提示
    printf("Successfully copied man page.\n");

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值