I have this below small code where the file name is passed from command line. This program is getting called by execlp()
from a different program. I want the o/p of the command getting called by system()
here into the filename coming as argv[2]
. Not getting a clue how to do that.
#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;
}
The o/p of ./linked_list is simply a list getting printed as below:
****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...
Remember that the system
function calls the shell, so you can use normal shell redirection:
char command[64];
snprintf(command, sizeof(command), "./linked_list > %s", argv[2]);
system(command);