Store command output into a variable.

This is a discussion on Store command output into a variable. within the C Programming forums, part of the General Programming Boards category; Hi all, I'm looking for some help in storing the output of a system command into a variable. For instance, ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    18

    Store command output into a variable.

    Hi all,

    I'm looking for some help in storing the output of a system command into a variable. For instance, i want to store the output of the linux command 'md5sum myfile' into a variable so that i can compare it with other variables & print to the screen them.

    I have searched the board, and found a few examples, but i can't seem to get them to work. A few seem to be using the popen function.

    I would be greatful if somebody could help me out.

    Thank you for your time,
    Regards,
    Kevin

  2. #2
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Is this from inside your program? Or just from the command prompt?
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    18
    From inside my program.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    18

    That worked great

    Thankyou very much dear sir, the help is very much appreciated.

    Thankyou,
    Regards,
    Kevin

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    18
    Hi,

    I thought i'd post on the end of this one, rather than making a new thread.

    I'm trying to compare a variable, which stores the md5sum of a file, to a variable which already has the md5sum within it. Here's what i have-


    Code:
    #include <stdio.h>
    
    int main()
    {
    FILE *fd=popen("md5sum /home/kj/myfile","r");
    char buf[128];
    char buf2[]= "6aad283ce232c415b61e1d8c190c2b38  /home/kj/myfile";
    
    if(fd) {
    while(fgets(buf,128,fd))
           if(buf2==buf)
    	 printf("md5sum matches");
     printf("\nThe md5sum stored in buf is : %s",buf);
    
    }
     pclose(fd);
    }

    The problem is, both the variables store the same md5sum, but it doesn't print the message , md5sum matches to the screen. What's wrong with it?

    Thankyou for your time.

    Regards,
    Kevin.j

  6. #6
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Do you want to append this to the existing file- if so change your mode in the open

    Is this a typo?
    Code:
    FILE *fd=popen("md5sum /home/kj/myfile","r");\
    shouldn't popen be open?

    Mr. C.

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    18
    Hi,

    Thank you all, for participating.

    Salem, Thankyou for pointing that out, and pointing me in the direction of the thread, I was wondering why it always returns 'Doesn't match' with -

    Code:
    #include <stdio.h>
    
    int main()
    {
    FILE *fd=popen("md5sum /etc/passwd","r");
    char buf[128];
    char buf2[] = "6aad283ce232c415b61e1d8c190c2b38  /etc/passwd";
    
    if(fd) {
    while(fgets(buf,128,fd))
    
           if(strcmp(buf,buf2)==0)
    	 puts("\nMatches\n\n");
    	 else puts("\nDoesn't match\n\n");
    	
    }
     pclose(fd);
    }


    but i am unable to incorporate the example(s) into my program, or should i say i dont know how to. Here is how Ive tried to use it:

    Code:
    #include <stdio.h>
    
    int main()
    {
    FILE *fd=popen("md5sum /etc/passwd","r");
    char buf[128];
    char buf2[] = "6aad283ce232c415b61e1d8c190c2b38  /etc/passwd";
    char *p = strchr(buff, '\n'); if(p!=null) *p='\n';
    
    if(fd) {
    while(fgets(buf,128,fd))
    
           if(strcmp(buf,buf2)==0)
    	 puts("\nMatches\n\n");
    	 else puts("\nDoesn't match\n\n");
    	
    }
     pclose(fd);
    }

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    18
    It works a treat vVv . Thankyou very much guys, it's appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  2. store date in a variable
    By actionbasti in forum C++ Programming
    Replies: 2
    Last Post: 10-01-2003, 09:50 PM
  3. newb question~
    By Anima in forum C++ Programming
    Replies: 10
    Last Post: 08-24-2003, 05:54 PM
  4. float/double variable storage and precision
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2003, 06:23 PM
  5. Im so lost at . .
    By hermit in forum C Programming
    Replies: 18
    Last Post: 05-15-2002, 01:26 AM

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