-1

I would like to know if there is a way to store the things that system prints out when you call some function from it for e.g. i want my program to read my Wi-Fi key/everything that comes out of console and store it in a .txt file.Is that possible? If not is printing out strings from system possible?

Here is the code for printing out the contents:

#include <stdio.h>


int main()
{
    system("netsh wlan show profile wifi name key=clear");



    return 0;
}
  • redirecting the output string to a file ptr would do the trick – Ratul Sharker Sep 16 '17 at 18:41
  • Will try it thanks for the suggestion! –  Sep 16 '17 at 18:43
  • 4
    On POSIX systems you might want to use [popen](http://pubs.opengroup.org/onlinepubs/009695399/functions/popen.html); while `system` is standardized by C, how the command is executed is not and is operating system specific. – Basile Starynkevitch Sep 16 '17 at 18:44
  • 1
    Possible duplicate of [How can I run an external program from C and parse its output?](https://stackoverflow.com/questions/43116/how-can-i-run-an-external-program-from-c-and-parse-its-output) – Cloud Sep 16 '17 at 19:07
  • Looks like an XY problem. Use a library for what you actually want to do with that call and call the functions directly. – too honest for this site Sep 16 '17 at 21:01

1 Answers1

-1

I figured it out using a much simpler way than @Ratul Sharker and @Basile Straynkevitch suggested. The solution is using > in the system, but you must have a .txt file before compilation(might figure out a way for the program to write the file)

Code:

#include <stdio.h>


int main()
{
    system("netsh wlan show profile wifi name key=clear > C:\\somepath\\name.txt");



    return 0;
}
  • Using popen() seems more elegant to me, because it avoids the need for a temporary file, and doesn't rely on particular shell behaviour. Also it avoid the extra memory overhead of loading a shell. – Kevin Boone Sep 16 '17 at 19:24