Thread: How do you read gzip output from 'C' program?

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    31

    How do you read gzip output from 'C' program?

    Hi,
    I'm coding in "C" and I am using the PIPE method to invoke the running of a gzip command from another program. Its a backup program and part of the program is to verify the integrity of the gzipped backup file. To do this I try to execute the command:
    "gzip -vt <backupfile>.tar.gz" via opening a PIPE.
    The pipe opens and executes this command fine however the resulting output doesn't get sent back up the PIPE to my program, it just gets printed out in the shell. E.g. "<backupfile>.tar.gz: OK" shows on the screen, but my program doesn't get this output sent back to it. So my program never knows the result of this test. How can I get my program to read this shell outputted result?

    Any help greatly appreciated.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    For some reason, gzip writes to stderr rather than stdout.

    How exactly are you invoking it?

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    31

    Follow Up

    Just a basic PIPE:

    Code:
    int c1=0;
    FILE *pi;
    char temp[1000]={'\x00'};
    char backupfilename[1000]={"backup.tar.gz"};
    char c;
    
    sprintf(temp, "gzip -vt %s", backupfilename);
    pi=popen(temp, "r");
    if(pi != NULL)
            {
            c=fgetc(pi);
            while(c != '\xff')
                    {
                    temp[c1]=c;
                    c=fgetc(pi);
                    c1++;
                    }
            temp[c1]='\x00';
            pclose(pi);
            }
    The code then goes on the parse whats in the temp array, looking for the string "OK", so the program knows its ok. But of course the temp array is empty as the output from gzip isn't sent to STDOUT as you've said.

    So how can I get the output from gzip into my temp array?

    By the way many many thanks for your assistance, really is appreciated!!!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > sprintf(temp, "gzip -vt %s", backupfilename);
    This might work to redirect stderr to stdout.
    sprintf(temp, "gzip -vt %s 2>&1", backupfilename);

    If not, then you'd need to use fork()/execl()/dup() to get control of the stderr descriptor for yourself.

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    31

    Yes

    Yes it WORKS

    THANK YOU, THANK YOU, THANK YOU.

    You are the best, I always get excellent help around these boards. Thanks a million !!!!!!!!!!!!!!!!!!!!!!!!!!

  6. #6
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Why don't you just use system("gzip -t <filename>") and look at the return value from system() to see if the archive is okay or not?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Gnome GTK / Spawn CLI app - read output
    By MtOzOr in forum Linux Programming
    Replies: 7
    Last Post: 09-08-2008, 06:41 AM
  2. Basic C input output program help
    By trevordunstan in forum C Programming
    Replies: 2
    Last Post: 01-27-2008, 06:41 PM
  3. Calling external program and reading its output.
    By Dragoon_42 in forum C++ Programming
    Replies: 3
    Last Post: 10-18-2007, 05:34 AM
  4. Replies: 3
    Last Post: 09-05-2005, 08:57 AM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM