How can I call two C applications from within another C application?
e.g. :
pg1.c can be run as ./a.out pg1_args
pg2.c can be run as ./a.out pg2_args
I would like to write a program that can be run as:
./a.out pg1_args pg2_args
With the result being equivalent to :
./a.out pg1_args
./a.out pg2_args
./a.out pg1_args
./a.out pg2_args
the pg1 here is svm_scale and pg2 here is svm_predict , both taken from libsvm : http://www.csie.ntu.edu.tw/~cjlin/libsvm/
[ edit ]
@Jonathan ,
I wrote these programs for trying out this concept..
pg1.c
#include <stdio.h>
#include <string.h>
int main(int argc,char **argv)
{
FILE *fin;
fin=fopen("pg1file.txt","a");
fprintf(fin,"%s",argv[1]);
fflush(fin);
fclose(fin);
}
pg2.c
#include <stdio.h>
#include <string.h>
int main(int argc,char **argv)
{
FILE *fin;
fin=fopen("pg2file.txt","a");
fprintf(fin,"%s",argv[1]);
fflush(fin);
fclose(fin);
}
pg3.c :
#include<stdio.h>
#include<string.h>
int main(int argc,char **argv)
{
int i;
const char *cmd1 = strcat("./pg1 ",argv[1]);
const char *cmd2 = strcat("./pg2 ",argv[2]);
for(i=0;i<4;i++)
{
if (system(cmd1) != 0)
printf("\n error executing pg 1");
if (system(cmd2) != 0)
printf("\n error executing pg 2");
}
}
[root@localhost trinity]# ./a.out first second
Segmentation fault (core dumped)
[root@localhost trinity]#
Could somebody explain what I've done wrong?