Suppress output from popen ()

Is there a way to suppress the output from popen() without losing Wait ().

Test 1:

 FILE * stream = NULL; char buffer [120]; stream = popen ("ffmpeg -y -i test.amr -ar 16000 test.wav -v quiet", "r"); while (fgets (buffer, sizeof(buffer), stream)) { } pclose (stream); 

Test 2:

 FILE * stream = NULL; char buffer [120]; stream = popen ("ffmpeg -y -i test.amr -ar 16000 test.wav -v quiet &> /dev/null", "r"); while (fgets (buffer, sizeof(buffer), stream)) { } pclose (stream); 

The problem with Test 2 is that pclose() does not wait for the pipe to complete processing. I don’t want to have a bunch of FFMPEG output every time I have to make a call.

+4
source share
2 answers

You should only use popen() when you want to send data or read data from a child process (and this is an exception - or if you want to do both), you must make the connection yourself).

If you do not want to do this, do not use popen() .

As jim mcnamara is precisely explained , given that you redirect the output of the child to /dev/null after creating the channel, redirecting closes the input channel into your program, so popen() gets zero bytes to read, which is considered EOF. And he comes back - for him there is nothing more readable (if there could be more for him, he would not receive EOF).

In this context, use system() ; he will wait for the finish to finish - even when the output of the child is redirected to /dev/null . In other contexts, it would be wise to use lower-level fork() and exec*() .

+2
source

the output in / dev / null means that popen (which calls the read () function) does not block the private file descriptor for stdout. He returns immediately.

You effectively close stdout by redirecting it (dup ()), in fact it returns EOF

+1
source

Source: https://habr.com/ru/post/1397139/


All Articles