1

I'm writing a program in C, in this model:

//Block 1: Does a lot of operations, including debug messages

strcpy(call, "xdg-open ");
strcat(call, url);

system(call);

//Block 2: Does a lot of operations, including debug messages

After the invocation of system(), it looks like this function takes control of stdout and Block 2 debug messages aren't printed anylonger, only Block 1 messages.

Does anybody know what is happening and what might be the solution for it?

amportugal
  • 125
  • 1
  • 10

1 Answers1

0

When you execute some other application using int system(const char *command) function, your application will wait until the called one exits. That is the reason why you do not see any outputs from Block 2.

You could solve this by adding & at the end off command which will cause whole application will run as different process. So you can try this:

strcpy(call, "xdg-open ");
strcat(call, url);
strcat(call, " &");

system(call);

Also you may need to flush stdout buffer, so call fflush(stdout) after Block 2.

Firzen
  • 1,599
  • 7
  • 24
  • 41
  • Doing the same result, I've tried that before. It seems the command's execution finishes but stdout remains with the same. – amportugal Sep 20 '16 at 10:55
  • Try to call `fflush(stdout)` after printing debug messages. – Firzen Sep 20 '16 at 11:02
  • It solves the problem temporarily, but this is one function of a very complex program, its annoying having to do that to print the debug messages. – amportugal Sep 20 '16 at 11:11
  • 1
    This is just feature of `stdout`. You can always define your own debugging function which will flush buffer after every single debug message. See this if you want to know more about it: http://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin – Firzen Sep 20 '16 at 11:32