Skip to content

Instantly share code, notes, and snippets.

@NigoroJr
Last active August 29, 2015 14:19
Show Gist options
  • Save NigoroJr/1a58862b419f27c3bf44 to your computer and use it in GitHub Desktop.
Save NigoroJr/1a58862b419f27c3bf44 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <fstream>
#include <string>
int main(const int argc, char* const argv[]) {
std::streambuf* buf;
if (argc >= 2) {
std::cout << "Got two or more args" << std::endl;
// OK
std::ofstream ofs;
ofs.open(std::string(argv[1]));
// Not OK
// std::ofstream ofs(std::string(argv[1]));
buf = ofs.rdbuf();
}
else {
std::cout << "Using std::cout" << std::endl;
buf = std::cout.rdbuf();
}
std::ostream os(buf);
os << "Hello, World!" << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment