how to directly store cmd/prompt outputs to a variable, without using file operations ???

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 a Windows-oriented program 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"); ... ?
Use popen():
POSIX:
http://pubs.opengroup.org/onlinepubs/009695399/functions/popen.html

MS Windows:
https://msdn.microsoft.com/en-us/library/96ayss4b.aspx

If you have Boost available, there is a Boost iostream which can adapt a file descriptor. This might make things a little easier:
http://www.boost.org/doc/libs/1_63_0/libs/iostreams/doc/classes/file_descriptor.html
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <string>
#include <stdio.h>
#include <memory>

#if defined _WIN32 || defined __WIN64
    #define popen _popen
    #define pclose _pclose
#endif // _WIN32 || __WIN64


std::string exec_cmd( std::string cmd )
{
    std::unique_ptr< ::FILE, decltype(&::pclose) > pipe( ::popen( cmd.c_str(), "r" ), &::pclose ) ;

    if(pipe)
    {
        std::string result ;

        static constexpr std::size_t BUFFSZ = 256 ;
        char buffer[BUFFSZ] ;
        while( ::fgets( buffer, BUFFSZ, pipe.get() ) ) result += buffer ;

        return result ;
    }

    else return {} ; // failed, return empty string
}

http://coliru.stacked-crooked.com/a/8f0c659dc0e673ae
http://rextester.com/LVHHY2425
Alternatively,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# include <boost/iostreams/device/file_descriptor.hpp>
# include <boost/iostreams/stream.hpp>

# include <stdio.h>
# include <iostream>

using Stream = boost::iostreams::stream
                 <boost::iostreams::file_descriptor_source>;

int main() { 
    auto fd = ::popen("echo dlrow olleh | grep -Eo olleh | rev", "r");   
    Stream s{::fileno(fd), boost::iostreams::close_handle};
    for (std::string line; std::getline(s, line);)
        std::cout << line << '\n';    
}


http://coliru.stacked-crooked.com/a/a377a81d866683d7
Last edited on
Topic archived. No new replies allowed.