emulate zcat -d command through c?

This is a discussion on emulate zcat -d command through c? within the C Programming forums, part of the General Programming Boards category; I am just wondering how to go about emulating zcat -d command through c? normally I would do something like ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    35

    emulate zcat -d command through c?

    I am just wondering how to go about emulating zcat -d command through c?

    normally I would do something like

    Code:
    zcat -d filename | grep something
    but I wanted to do this through c programming and wondering if there is anyway internally to do it through c ?

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,145
    I suppose you first need to find a library that implements whatever algorithm "compress" uses, and just read the file, feed it into the library, and output it.

    Why would you want to reinvent the wheel, though?

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Code:
    FILE* f = popen("zcat -d filename | grep something", "r");
    if(f)
    {
        char buffer[256];
        while(fgets(buffer, sizeof(buffer), f))
        {
            printf("%s", buffer);
        }
        fclose(f);
    }

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    35
    thanks guys,
    I will play w/ bithub's code and see what I can learn from it..
    I guess i don't want to emulate the zcat... i just didn't know how to do command line(using other linux program to feed to c programs.. I should have been more clear.

    I am a newbie to c but not to programming.
    I just want to see how things gets done in c programming.

    I am also trying to recover some actual c source code on simple programs like sort, cut and things like that.
    What is the easiest way to get to them? I am looking into rpm source right now as I am fedora guy.. but trying to see if there are easier ways(website w/ all the source codes ?

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,145
    ftp://mirrors.kernel.org/gnu/

    Or any of the other GNU mirrors.

    The commands you looking for should be in coreutils.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    35
    thanks!

  7. #7
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    957
    I am also trying to recover some actual c source code on simple programs like sort, cut and things like that.
    http://www.freebsd.org/cgi/cvsweb.cgi/src/usr.bin/
    The Heirloom Project
    Home at OpenSolaris.org
    ftp://ftp.gnu.org/gnu/

    There are various implementations of POSIX utilities. The above are links to FreeBSD's, Gunnar Ritter's (based mainly on the original unix source code), Solaris's (also based on unix, though I'm not sure how many changes they've acquired) and GNU's versions

    Some of FreeBSD's tools can also be found in src/bin, src/sbin, and src/usr.sbin.

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    35
    what is wrong w/ below code?

    Code:
    #include <stdio.h>
    
    main( int argc, char *argv[]  )
    {
        printf( "filename is %s\n", argv[1]);
        FILE* f = popen("zcat -d argv[1] | grep something", "r");
        if(f)
        {
            char buffer[256];
            while(fgets(buffer, sizeof(buffer), f))
            {
                printf("%s", buffer);
            }
            fclose(f);
        }
    }
    I keep getting this..

    Code:
    [root@myserver c]# ./zcat_split todaysfile_1.gz
    filename is todaysfile_1.gz
    gzip: argv[1].gz: No such file or directory

  9. #9
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,145
    Code:
    FILE* f = popen("zcat -d argv[1] | grep something", "r");
    You are passing in "argv[1]" literally.

    Use strcat or something.

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    35
    digging more...

    Code:
    #include <stdio.h>
    #include <string.h>
    
    main( int argc, char *argv[]  )
    {
        char filename[29];
        char file_n[50];
        printf( "filename is %s\n", argv[1]);
        strcat(file_n,argv[1]);
        printf( "now filename is %s\n", file_n);
    
        FILE* f = popen("zcat -d file_n | grep something", "r");
        if(f)
        {
            char buffer[256];
            while(fgets(buffer, sizeof(buffer), f))
            {
                printf("%s", buffer);
            }
            fclose(f);
        }
    }
    result

    Code:
    filename is todaysfile_1.gz
    now filename is todaysfile_1.gz
    gzip: file_n.gz: No such file or directory

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    you cannot strcat to uninitiaized string...

    random calling to some functions without understanding your goal will not help...

    Code:
    char command[100];
    sprintf(command, "zcat -d %s | grep something", argv[1]);
    popen(command,
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    35
    thanks..

    I am trying things out without really understanding is correct. but lot of times, this is how I learn the best.. by doing. and confirmation what I dont understand and what i do understand..

    But you guys keep giving me new ideas to study on and trying out other things.. so it's great!!
    I will come back with something better.. (solution or approaching solution)

  13. #13
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,145
    For longer code, that won't work...

    You can't just "try out" a few thousand lines of code, and hope it works.

    Understanding is really the way to go.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by cyberfish View Post
    For longer code, that won't work...

    You can't just "try out" a few thousand lines of code, and hope it works.
    You can. Well I can. Or did rather.

    DIKU, MERC and ENVY taught me C.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    if(f)
        {
            char buffer[256];
            while(fgets(buffer, sizeof(buffer), f))
            {
                printf("%s", buffer);
            }
            fclose(f);
        }
    Or may be before you read then on to the buffer, you need to allocate the member. Use fseek and ftell to find the file size and then allocate enough memory for the buffer or may be just reach each char and the print it on to the standard output.

    Code:
    fseek(f, SEEK_SET, SEEK_END);
    size = ftell(f);
    or

    Code:
    do
    {
        fputc(f)
    } while( ( ch = fgetc(f) ) != EOF );
    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Emulate an '@' keypress or any other char
    By Kirdra in forum Windows Programming
    Replies: 6
    Last Post: 02-04-2009, 08:07 AM
  2. Program to emulate calculator
    By crucified in forum C Programming
    Replies: 24
    Last Post: 09-09-2008, 01:58 AM
  3. Control characters to emulate a prompt?
    By xconspirisist in forum Windows Programming
    Replies: 3
    Last Post: 02-08-2006, 03:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21