JPGygax68
2/6/2015 - 9:00 PM

How to support piping (input/output redirection) in C++

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;
}