1

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?

1 Answer 1

3

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).

6
  • 1
    If that's the case then it's not the dd command but one of the others that's writing to standard error. You could just redirect standard error to standard output for the entire pipe of commands by changing your command to the following: (gzip -dc backup/backup.img.gz | pv -ptrbe -i 2 -s 2339876653 | dd of=/dev/sdb) 2>&1
    – user335938
    Commented Dec 22, 2010 at 11:02
  • Now the behavior is little bit changed, and no output is printed on terminal but the code is still halted on same line... it means that output is being redirected but why it is not being redirected to PIPE? Commented Dec 22, 2010 at 12:06
  • Do you see all the output printed by your program once the dd operation completes?
    – user335938
    Commented Dec 22, 2010 at 12:13
  • 1
    pv doesn't write progress report to standard error if it's not a terminal (i.e. isatty(2) == 0) unless forced by -f option.
    – rkhayrov
    Commented Dec 22, 2010 at 12:33
  • yes, but not all the output, only the last two lines of output are printed by my code, i.e. 5533841+0 records out 5533841+0 records in and total bytes copied, but no progress information is received by the code.. Commented Dec 22, 2010 at 12:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.