Thread: emulate zcat -d command through c?

  1. #16
    Registered User
    Join Date
    Feb 2009
    Posts
    35
    cyberfish, I certainly understand and appreciate your concern.

    Sometimes, I don't understand such simple as

    system("command", "arg1", "arg2"); where arg1 or arg2 can be variables..
    in perl
    would be so difficult to execute in c programming but I certainly enjoy the challenge of programming and definitely look forward to learn more in programming in c.

    As to trying things out piece by piece of code is perhaps due to my perl background but I certinaly think actually typing and see what compiler spits out either confirms what I know or what i dont know and i think it helps..

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The system function just takes a single string as an argument. If you need the contents of members of argv, then you will need to first pass those to a buffer to prepare them for output. A variable, and a string which is the same as the name of a variable, are usually not the same thing with regards to their usefulness.
    Code:
    int x = 5;
    ...
    printf( "x" );
    That doesn't print 5. That's the point they were trying to make earlier.


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

  3. #18
    Registered User
    Join Date
    Feb 2009
    Posts
    35
    sorry, Quzah, but I just tried your code and it actually printed 5.. ?

    Code:
    [root@Xclient c]# cat variable_print.c
    #include <stdio.h>
    
    main()
    {
      int x = 5;
      printf("x");
    }
    result
    Code:
    [root@Xclient c]# gcc -o variable_print variable_print.c
    [root@Xclient c]# ./variable_print
    x[root@Xclient c]#

  4. #19
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Uhh...

    where do you see a 5 in that?

  5. #20
    Registered User
    Join Date
    Feb 2009
    Posts
    35
    my bad,

    of course only below code would product 5.. I am still trying to see what everyone was talking about earlier.. will get back to you. thanks

    Code:
    #include <stdio.h>
    
    main()
    {
      int x = 5;
      printf("x");
      printf("%i",x);
    }

  6. #21
    Registered User
    Join Date
    Feb 2009
    Posts
    35
    so far this is working fine for me..

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

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, 09: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, 04:16 PM