how to directly store cmd/prompt outputs to a variable without creating file??

May 2, 2017 at 10:24pm
I'm new here so forgive me if I post my question in the wrong place. Part of one of my programs filters the CMD output of "netsh wlan show networks bssid" into a txt file on my desktop, before using fstream to read each string of the file, pushing back each string into a vector of strings, and then deleting the txt file on the desktop before continuing. Here is an example:

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
string cmd_output;
vector<string> output;
fstream search;
system("netsh wlan show networks bssid > cmd_output.txt");
search.open("cmd_output.txt", ios::in);
while (search >> cmd_output)
output.push_back(cmd_output);
search.close();
system("del cmd_output.txt");
//program continues..
return 0;
}

Now ... If I wanted to simply filter the CMD output into a vector (without creating any txt files), how might I go about doing that? I've tried declaring variables and assigning the system calls to them but that doesn't work.

How might one go about trying something like ...
string cmd_output = system("netsh wlan show networks bssid"); ... ?
Last edited on May 2, 2017 at 10:27pm
May 3, 2017 at 3:16am
How might one go about trying something like ...
string cmd_output = system("netsh wlan show networks bssid"); ... ?

The system() function has no such capability. The function you're looking for is the POSIX function popen(). The boost library collection recently got a very convenient iostreams-based wrapper around it, see boost.process http://www.boost.org/doc/libs/1_64_0/doc/html/process.html

Based on "netsh" and "del", you're on Windows, the Windows analog is _popen() - see https://msdn.microsoft.com/en-us/library/96ayss4b.aspx
Topic archived. No new replies allowed.