STinC++ rewrites the programs in Software Tools in Pascal using C++
Having made particular reference to test-driven development when talking about include
, I’m going to turn around and say I didn’t bother for this next program, concat
. It’s so simple, I just went straight in. Now this is dangerous talk, because even the simplest seeming code can have awkward corners, but in this case I think I’m ok and in a few moments, I hope you’ll agree.
concat
concatenates a set of named input files onto the console. Kernighan and Plauger cite the example of combining multiple files together to be a piped into a program that can only read its standard input. You’ve used cat
- you know what’s going on here.
We can put concat
together using two functions we’ve already written - make_arguments
from echo
mid-way through Chapter 2 and used frequently since, combined with copy
from waaay back last August in the very first program of the first chapter.
#include <iostream>
#include <fstream>
#include "../../lib/arguments.h"
#include "../../lib/copy.h"
int main(int argc, char const* argv[]) {
auto filenames = stiX::make_arguments(argc, argv);
for (auto filename : filenames) {
std::ifstream file(filename);
if (!file) {
std::cerr << "Could not open '" << filename << "'\n";
continue;
}
stiX::copy(file, std::cout);
}
}