Skip to content

Instantly share code, notes, and snippets.

@JPGygax68
Last active September 18, 2017 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JPGygax68/6a0ae711a1b619f0227d to your computer and use it in GitHub Desktop.
Save JPGygax68/6a0ae711a1b619f0227d to your computer and use it in GitHub Desktop.
How to support #piping (input/output redirection) in C++
ifstream ifs;
ofstream ofs;
istream *is = nullptr;
ostream *os = nullptr;
if (!input_file.empty()) {
ios_base::sync_with_stdio(false);
ifs.open(input_file, ios::binary);
is = &ifs;
}
else {
is = &cin;
}
if (!output_file.empty()) {
ios_base::sync_with_stdio(false);
ofs.open(output_file);
os = &ofs;
}
else {
os = &cout;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment