Like Tree6Likes
  • 1 Post By Salem
  • 3 Post By quzah
  • 2 Post By theitsmith

Search command output for string

This is a discussion on Search command output for string within the C Programming forums, part of the General Programming Boards category; Hi, I'm very new to C programming. What I'm attempting to do is to run a command in Windows and ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    7

    Search command output for string

    Hi,

    I'm very new to C programming.

    What I'm attempting to do is to run a command in Windows and then parse the output of that command for a specific string of characters. Here is the code that I have working--it simply prints the output of the command.

    Code:
    #include <stdio.h>
    
    int main()
    {
        FILE *fp;
        char path[1035]; // Set path array size to 1035.
    
    
        fp = popen("ipconfig", "r"); // Open the command.
        if(fp == NULL) { // If nothing is returned, error out.
            printf("Failed to run command\n");
        }
    
    
        while(fgets(path, sizeof(path) - 1, fp) != NULL) { // Get output from command and print it.
            printf("%s", path);
        }
    
    
        pclose(fp);
    
    
        return 0;
    }
    What I'd like this program to do is parse the output of that command (rather than print it), searching for specific IP addresses. If it finds the searched-for IP address(es), I would like it to print those values. As yet, I have been unable to do this.

    Any pointers would be greatly appreciated!

    ~ Tom

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    Look up strcmp() and strstr() to begin with.
    theitsmith likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    7
    Quote Originally Posted by Salem View Post
    Look up strcmp() and strstr() to begin with.
    Thanks Salem! Looks like strstr was what I needed! I had tried strcmp previously but was unable to get it to return the value I was looking for.

    Here's what I came up with:

    Code:
    #include <stdio.h>
    
    
    int main()
    {
        FILE *fp;
        char path[1035]; // Set path array size to 1035.
    
    
        fp = popen("ipconfig", "r"); // Open the command.
        if(fp == NULL) { // If nothing is returned, error out.
            printf("Failed to run command\n");
        }
    
    
        while(fgets(path, sizeof(path) - 1, fp) != NULL) { // Get output from command and print it.
            //printf("%s", path);
    
    
            // Search for our substrings
            const char *lgString = path;
            const char *smString = "IPv4";
            char *ptr;
            ptr = strstr(lgString, smString);
    
    
            if(ptr) { // If found, filter out relevent strings by IP
                const char *lgString2 = ptr;
                const char *smString2 = "xxx.xxx.xxx";
                char *ptr2;
                ptr2 = strstr(lgString2, smString2);
    
    
                if(ptr2) { // If found, print the resulting IP.
                    printf("Your IP address is: %s", ptr2);
                }
            }
        }
    
    
        pclose(fp);
        return 0;
    }
    So this works to give me the value that I'm looking for. But is there possibly a better way to do this (overall, not just regarding the while loop)? (Better meaning more efficient, cleaner, or whatnot.)

    As I mentioned, I'm new to C and always interested in seeing someone else's take on how to solve a particular problem. So if you're open to providing your feedback, I'd love to hear it!

    Thanks again!

    ~ Tom

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Don't include comments like "set path array size to 1035" since the code is obvious.
    You should exit with some non-zero value if the file doesn't open.
    Don't say "our" in your comments (since I had nothing to do with it).
    You don't need the -1 after sizeof(path). fgets knows it needs to save a character for the terminating null.
    You don't need the extra variables lgString, smString. They just clutter things up.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by oogabooga View Post
    Don't say "our" in your comments (since I had nothing to do with it).
    The award for Dumbest Nit Pick of the Day goes to ... YOU!


    Quzah.
    manasij7479, MK27 and twomers like this.
    Hope is the first step on the road to disappointment.

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by quzah View Post
    The award for Dumbest Nit Pick of the Day goes to ... YOU!
    Awesome! Thanks!
    It's a pet peeve of mine to see the pretentious "our" in comments.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    7
    Quote Originally Posted by oogabooga View Post
    Don't say "our" in your comments (since I had nothing to do with it).
    "Our" wasn't referencing YOU and me--the ideas I'm using to experiment with C have already been put to use in some scripts that I've created for my employer. So "our" more generally refers to anyone (who works for my employer) who might run this program or view the code.

    Thanks for the feedback, though! I'll keep your other suggestions in mind. :-)
    quzah and twomers like this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String incorrect for output command target
    By DarkAlex in forum C++ Programming
    Replies: 16
    Last Post: 08-19-2008, 09:32 PM
  2. Binary Search - Strange Output
    By mike_g in forum C Programming
    Replies: 13
    Last Post: 06-16-2007, 02:55 PM
  3. String Search with command line arguments
    By goron350 in forum C Programming
    Replies: 5
    Last Post: 11-29-2004, 04:56 PM
  4. system command's output
    By modec in forum C Programming
    Replies: 3
    Last Post: 08-17-2003, 06:21 PM
  5. system command output.
    By joshk in forum C Programming
    Replies: 3
    Last Post: 08-21-2002, 01:49 PM

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