0

我在下面的小代码中有这个文件名是从命令行传递的。这个程序被execlp()另一个程序调用。我希望命令的 o/p 在system()这里被调用到文件名中argv[2]。不知道如何做到这一点。

#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;
}

./linked_list 的 o/p 只是一个打印出来的列表,如下所示:

****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...
4

1 回答 1

3

请记住,该system函数调用了 shell,因此您可以使用正常的 shell 重定向:

char command[64];
snprintf(command, sizeof(command), "./linked_list > %s", argv[2]);
system(command);
于 2013-08-27T06:42:30.007 回答