I'm quite new at C programming. Im using a Mac and I just created this app program with Matlab. This app what it does is, that creates a .txt based on some data. When I run it directly from the terminal it creates my .txt file, but when I try to execute this app using the following code (First test code) :
int main(int argc, const char * argv[])
{
char test_Exe[BUFSIZ];
// Build a buffer, and execute the commands within it
strcpy (test_Exe, "/Users/pepe1503/Documents/Pepe/Stage_Bruker/HS_Conversion_Topspin_Bucket_Matlab/matlab/MP_ToolBox/small_toolbox/test_Exe_D2O/distrib/run_test_Exe_D2O.sh");
strcat (test_Exe, " /Applications/MATLAB/MATLAB_Compiler_Runtime/v715 /Users/pepe1503/Documents/Pepe/Stage_Bruker/HS_Conversion_Topspin_Bucket_Matlab/matlab/MP_ToolBox/small_toolbox/D20/nmr/Agrifood_Caviar_Sturgeon_D2O/4/pdata");
printf ("Executing your Matlab program: %s\n", test_Exe);
system (test_Exe);
return 0;
}
I got the same printed output as the terminal but it doesn't build my .txt file.
Does anyone know the reason?
TY!
Following your suggestions I recently changed my code to (keeping my last program in another code just in case) :
int main(int argc, const char * argv[])
{
char* arg_list[] = {
"run_test_Exe_D2O.sh",
"/Applications/MATLAB/MATLAB_Compiler_Runtime/v715",
"/Users/pepe1503/Documents/Pepe/Stage_Bruker/HS_Conversion_Topspin_Bucket_Matlab/matlab/MP_ToolBox/small_toolbox/D20/nmr/Agrifood_Caviar_Sturgeon_D2O/4/pdata",
NULL
};
pid_t child_pid;
child_pid = fork();
if ( child_pid != 0 ) {
// This is the parent process.
return child_pid;
}
else {
// Now execute PROGRAM, searching for it in the path.
fprintf(stdout, "\n");
fprintf(stdout, "Executing your Matlab program:\n");
execvp("/Users/pepe1503/Documents/Pepe/Stage_Bruker/HS_Conversion_Topspin_Bucket_Matlab/matlab/MP_ToolBox/small_toolbox/test_Exe_D2O/distrib/run_test_Exe_D2O.sh", arg_list);
// The execvp function returns only if an error occurs.
fprintf (stderr, "an error occurred in execvp\n");
abort ();
}
return 0;
}
It still executes the application, now not printing the messages of the application as with system(), but it stills not create my .txt that I can obtain by executing it from my terminal. To be more precise with system() it does create my .txt but it erase it after the program execution.
Any sugestions of why it doesn't print my app messages and why it does create it but erase it ?