1

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.

1
  • system only runs a command and gives you its exit status. To get the actual output of the program read about the popen function (or _popen on Windows). Commented Aug 1, 2022 at 7:10

1 Answer 1

5

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;
}
Sign up to request clarification or add additional context in comments.

8 Comments

Why do you declare the popen and pclose functions? Both they and the Windows _popen and _pclose are declared in the stdio.h header file. You should not declare them yourself.
@Someprogrammerdude without a prototype gcc is warning: warning: implicit declaration of function ‘popen’ (at least on Linux)
With MS VC it's _popen() and _pclose() because they are not Standard C functions.
You really get the warnings when building on Linux? That's very odd. The official POSIX documentation quite clearly states that it should be declared in <stdio.h>. And I can't replicate it on my own (on different systems).
@DavidRanieri Yup, there's the issue. Use -std=gnu11 or #define the appropriate feature test macro before any includes.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.