1

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 ?

  • as @Remo.D already mentioned, apparently you have a problem with the string you are copying to test_Exe. But I would like to point out a couple of issues here: first, using `system()` is a horrible idea, use `fork()` and `exec()`. Second, in C a NUL termination is always needed at the end of strings. `strcpy` does not add the terminating null, therefore your program is prone to segmentation faults/buffer overflows. Use `strncpy(test_Exe, your_string_here. sizeof(test_Exe)-1)` instead – H_squared May 27 '14 at 12:28
  • What string do you expect test_Exe to hold? – H_squared May 27 '14 at 12:30
  • Hi @hhachem The program I want to execute is :"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" Using those two arguments: "/Applications/MATLAB/MATLAB_Compiler_Runtime/v715" and "/Users/pepe1503/Documents/Pepe/Stage_Bruker/HS_Conversion_Topspin_Bucket_Matlab/‌​matlab/MP_ToolBox/small_toolbox/D20/nmr/Agrifood_Caviar_Sturgeon_D2O/4/pdata". The total string : ; – user3679266 May 27 '14 at 14:15
  • @hhachem I'm quite new using C programming could you give me an example of how to use fork() and exec()? – user3679266 May 27 '14 at 14:20
  • check this link for an example: http://stackoverflow.com/questions/8371363/how-to-use-correctly-fork-and-exec – H_squared May 30 '14 at 06:31
  • @hhachem I just modified my code using what you told me, and just edited the question. It does execute my application but now it doesn't print what my application printed with system(). Also It does build the .txt but at the end of the compilation of my .c program it erase it (same with system), while when I execute my application using the terminal it doesn't erase it. Any idea? – user3679266 May 30 '14 at 09:37

3 Answers3

3

You are trying to execute the following program: "/Users/pepe1503/Documents/Pepe/Stage_Bruker/HS_Conversion_Topspin_Bucket_Matlab/matlab/MP_ToolBox/small_toolbox/test_Exe_D2O/distrib"

Are you sure this is what you intended?

Remo.D
  • 16,122
  • 6
  • 43
  • 74
  • Hey! Thank you for your answer: I'm trying to execute: /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 It was a mistake from my part. Using those two arguments: /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 – user3679266 May 27 '14 at 14:01
0

Try to use "bash run_test_Exe_D2O.sh" or "sh run_test_Exe_D2O.sh" instead raw script filename. Also, are you sure that whitespaces in script file name are OK?

  • when I try to use just "bash run_test_Exe_D2O.sh" I got this error: "bash: run_test_Exe_D2O.sh: No such file or directory" That's why I have to put all the path to my sh file – user3679266 May 27 '14 at 14:07
0

Ok I found the answer. The problem was with system that it creates I think a temporary file each time Xcode is lunched. In which it was storing my .txt files.