【发布时间】:2018-08-27 17:38:59
【问题描述】:
我正在执行一项任务,我需要运行系统命令并将输出写入文件。目前,我可以在运行时使用 >> output.txt 管道输出,但是如何在我的程序中自动执行此操作,而无需用户键入管道部分。我尝试将它连接到 system 函数本身,同时还尝试创建一个 temp 变量以将其附加到每个循环的开头。我已经很多年没有使用过 C 语言了,所以我很难找到一个相对容易的任务。这是我的源代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc, char *argv[]) { /*argc holds the number of arguments and argv is an array of string pointers with indifinate size */
/*Check to see if no more than 4 arg entered */
if(argc > 4 && argc > 0) {
printf("Invalid number of arguments. No greater than 4");
return 0;
}
FILE *fp;
int i;
char* temp[128];
for(i = 1; i < argc; i++) {
//strcopy(temp, argv[i]);
// printf("%s", temp);
system(argv[i] >> output.txt);
}
return 0;
}
感谢大家的帮助。
【问题讨论】:
标签: c operating-system pipe system