使用管道读取 dd 命令输出时出现问题

发布于 2024-10-08 15:36:29 字数 555 浏览 0 评论 0原文

我正在开发一个应用程序,在其中我想将 dd 命令的输出(进度信息)重定向到我的 C++ 程序,但它实际上并没有获得输出,这是代码,

FILE * progressInfo = popen("gzip -dc backup/backup.img.gz | pv -ptrbe -i 2 -s 2339876653 | dd of=/dev/sdb","r");
if(!progressInfo)
{
return -1;
}
char buf[1024];
while(fgets(buff, sizeof(buff),progressInfo)!=NULL)
{
    std::cout << buff << endl;
}

但问题是未收到进度信息buff,输出连续打印在终端上,上面的程序在 while(fgets(buff, sizeof(buff),progressInfo)!=NULL) 处暂停,如下一旦 dd 操作完成,循环块的下一行就会被执行。

如果有人知道为什么输出没有返回到 buff,并且在终端上不断重新调整?

I am developing an application, in which I want to redirect the output (progress information) of dd command to my C++ program, but it is not actually getting the output, here is the code

FILE * progressInfo = popen("gzip -dc backup/backup.img.gz | pv -ptrbe -i 2 -s 2339876653 | dd of=/dev/sdb","r");
if(!progressInfo)
{
return -1;
}
char buf[1024];
while(fgets(buff, sizeof(buff),progressInfo)!=NULL)
{
    std::cout << buff << endl;
}

but the problem is the progress information is not received in buff, and the output is continuously printed on terminal, and above program halts on while(fgets(buff, sizeof(buff),progressInfo)!=NULL), and as soon as the dd operation is completed, the very next line to loop block is executed.

if anyone has any idea why the output is not returned to buff, and its continuously retuned on terminal?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱你不解释 2024-10-15 15:36:29

输出可能被写入标准错误而不是标准输出。只需将“ 2>&1”添加到命令字符串的最末尾,您就应该看到输出(注意前导空格)。

The output is probably being written to standard error rather than standard output. Just add " 2>&1" to the very end of your command string and you should see the output (note the leading space).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文