Programmer
Programmer

Reputation: 11

How to get specific data from output of the command run through c?

system("netsh wlan show profile");

I executed this cmd using c program now, i want all profile names shown in the output and save it in variable(array). How to do that also please tell me in detailed because i don't have knowledge of c, i am trying to learning it.

Upvotes: 1

Views: 246

Answers (1)

David Ranieri
David Ranieri

Reputation: 41017

Use popen:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    const char *str = "netsh wlan show profile";
    FILE *cmd;

#ifdef _WIN32
    cmd = _popen(str, "r");
#elif __unix
    cmd = popen(str, "r");
#else
#   error "Unknown system"
#endif
    if (cmd == NULL)
    {
        perror("popen");
        exit(EXIT_FAILURE);
    }

    char result[1024];

    while (fgets(result, sizeof(result), cmd))
    {
        printf("output: %s", result);
    }
#ifdef _WIN32
    _pclose(cmd);
#elif __unix
    pclose(cmd);
#endif
    return 0;
}

Upvotes: 5

Related Questions