需求:将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;
}