C%2b%2b Ostream Dev Null

C%2b%2b Ostream Dev NullC%2b%2b Ostream Dev NullC 2b 2b ostream dev null key/*
# Copyright (C) 2008 David M. Whitney (aka 'dwhitney67')
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string>
#include <vector>
#include <algorithm>
#include <stdexcept>
#include <sstream>
#include <iostream>
class ExecCommand
{
public:
typedef std::vector<std::string> CmdResults;
ExecCommand( const std::string &cmd )
{
FILE *pfd = popen( cmd.c_str(), 'r' );
if (
pfd <= 0 )
{
throw
std::runtime_error( 'Command or process could not be executed.' );
}
while ( !
feof(pfd) )
{
char buf[ 1024 ] = {0};
if (
fgets(buf, sizeof(buf), pfd) > 0 )
{
std::string str(buf);
m_results.push_back( str.substr(0, str.length()-1) );
}
}
pclose( pfd );
}
const
CmdResults getResults() const
{
return
m_results;
}
friend std::ostream & operator<<( std::ostream &os, const ExecCommand &exec )
{
std::for_each( exec.m_results.begin(), exec.m_results.end(), ExecCommand::Displayer(os) );
return
os;
}
private:
class
Displayer
{
public:
Displayer( std::ostream &os ) : m_os(os) {}
void operator()( const std::string &str )
{
m_os << str << std::endl;
}
private:
std::ostream &m_os;
};
CmdResults m_results;
};
int main( int argc, char **argv )
{
try
{
std::string cmd;
for (
int i = 1; i < argc; ++i )
{
cmd += argv[i];
cmd += ' ';
}
std::cout << 'cmd = ' << cmd << std::endl;
ExecCommand exec( cmd );
const
ExecCommand::CmdResults results = exec.getResults();
std::cout << exec << std::endl;
}
catch (
std::exception &ex )
{
std::cerr << 'Error: ' << ex.what() << std::endl;
}
return
0;
}

C 2b 2b Ostream Dev Null Command

Questions: I’m looking for a std::ostream implementation that acts like /dev/null. It would just ignore anything that is streamed to it. Does such a thing exist in the standard libraries or Boost? Or do I have to roll my own? Answers: If you have boost, then there’s a null ostream & istream implementation available in. Internally, its ostream base constructor is passed a pointer to a newly constructed filebuf object (the internal file stream buffer). (2) initialization constructor Constructs an ofstream object, initially associated with the file identified by its first argument ( filename ), open with the mode specified by mode.