Suppress output from popen()

Suppress output from popen()

from stackoverflow


病例样本:

Is there a way to suppress the output from popen() without losing the 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() is not waiting for the pipe to finish processing. I don't want to have a bunch of FFMPEG output every time I have to do a pipe.


Answers:

--Sorry, I don't see the problem in the code. Could you post the actual output, describe the desired output, etc.? We programmers need input and output pipes too :) – uʍop ǝpısdn Feb 18 at 1:17

--Could ffmpeg be writing to stderr for some reason ? Could you tryffmpeg ... 2> /dev/null ? – cnicutarFeb 18 at 1:18

--@cnicutar Thanks 2> solved the issue! – Juan Gabriel Calderón-PérezFeb 18 at 1:23

--For future reference: &> redirects both standard-output and standard-error. To redirect only standard-output, use> or1>; to redirect only standard-error, use2>, as cnicutar suggested. – ruakhFeb 18 at 1:28

-- &> is nonstandard and should not be used.The standard way to redirect both is>/dev/null 2>&1 – R..Feb 18 at 1:57


--

You should only use popen() when you want to send data to, or read data from, the child process (and that is an exclusive-or; if you want to do both, you have to set up the connection yourself).

If you don't want to do that, don't use popen().

As jim mcnamara accuratelyexplained, given that you redirect the output of the child to/dev/null after the pipe is created, the redirection closes the pipe input to your program, sopopen() gets zero bytes to read, which counts as EOF. And it returns - there is nothing more for it to read (if there might be more for it, it would not have received EOF).

In this context, use system(); it will wait for the child to finish - even when the output of the child is redirected to/dev/null. In other contexts, it might be appropriate to use the lower-levelfork() andexec*() routines instead.


--

output to /dev/null means that popen (which is calling read() ) does not block on a closed file descriptor for stdout. It returns right away.

You effectively closed stdout by redirecting it (dup() ), in reality it returns EOF


Changelog:

* Apr 28 2012 Richard Qi <qihh85@126.com> 

- 如何判断子进程执行成功与否及获取错误信息

the following macros, which are defined in <sys/wait.h> and evaluate to integral expressions; the stat_valargument is the integer value pointed to by stat_loc.

WIFEXITEDstat_val)

Evaluates to a non-zero value if status was returned for a child process that terminated normally.

WEXITSTATUS( stat_val)

If the value of WIFEXITED(stat_val) is non-zero, this macro evaluates to the low-order 8 bits of the status argument that the child process passed to _exit() or exit(), or the value the child process returned from main().

WIFSIGNALED( stat_val)

Evaluates to a non-zero value if status was returned for a child process that terminated due to the receipt of a signal that was not caught (see <signal.h>).

WTERMSIG( stat_val)

If the value of WIFSIGNALED(stat_val) is non-zero, this macro evaluates to the number of the signal that caused the termination of the child process.

WIFSTOPPED( stat_val)

Evaluates to a non-zero value if status was returned for a child process that is currently stopped.

WSTOPSIG( stat_val)

If the value of WIFSTOPPED(stat_val) is non-zero, this macro evaluates to the number of the signal that caused the child process to stop.

WIFCONTINUED( stat_val)

Evaluates to a non-zero value if status was returned for a child process that has continued from a job control stop.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值