我有以下小代码,其中文件名从命令行传递。该程序由execlp()
从另一个程序调用。我希望system()
在argv[2]
调用命令的o / p到文件名为#include <stdio.h>
int main(int argc,char *argv[])
{
char c;
printf("Received : %s filename: %s\n",argv[1],argv[2]);
FILE *fp=fopen(argv[2],"w");
system("./linked_list");
printf("Here...\n");
sleep(5);
//sleep(3*atoi(argv[2]));
return 0;
}
。没有弄清楚如何做到这一点。
****printing list****
info :0 address:0x884e068 next:0x884e0c8
info :1 address:0x884e0c8 next:0x884e058
info :2 address:0x884e058 next:0x884e0b8
info :3 address:0x884e0b8 next:0x884e048
info :4 address:0x884e048 next:0x884e0a8
info :5 address:0x884e0a8 next:0x884e038
info :6 address:0x884e038 next:0x884e098
info :7 address:0x884e098 next:0x884e028
info :8 address:0x884e028 next:0x884e088
info :9 address:0x884e088 next:0x884e018
info :10 address:0x884e018 next:0x884e078
info :11 address:0x884e078 next:0x884e008
Last element... info:12 address:0x884e008 next:(nil)
Child terminating...
./linked_list的o / p只是一个列表打印如下:
{{1}}
答案 0 :(得分:3)
请记住,system
函数调用shell,因此您可以使用普通的shell重定向:
char command[64];
snprintf(command, sizeof(command), "./linked_list > %s", argv[2]);
system(command);