Video Coming Soon...
37: expand
Implement the first command line tool, expand.
View Source file expand.cpp Only
// https://www.gnu.org/software/coreutils/manual/html_node/expand-invocation.html
#include <fmt/core.h>
#include <fstream>
void convert_file(std::istream& in, int tab_stop) {
std::string in_line;
while(in) {
std::string out_line;
getline(in, in_line);
for(size_t i = 0; i < in_line.size(); i++) {
char ch = in_line[i];
if(ch == '\t') {
out_line.append(tab_stop, ' ');
} else {
out_line += ch;
}
}
fmt::println("{}", out_line);
}
}
int main(int argc, char* argv[]) {
for(int i = 1; i < argc; i++) {
std::ifstream in_file{argv[i]};
convert_file(in_file, 2);
}
}
Register for Learn C++ the Hard Way
Register to gain access to additional videos which demonstrate each exercise. Videos are priced to cover the cost of hosting.