Video Coming Soon...
42: uniq
Implement the first command line tool, uniq.
View Source file uniq.cpp Only
//https://www.gnu.org/software/coreutils/manual/html_node/uniq-invocation.html
#include <fmt/core.h>
#include <iostream>
#include <unistd.h>
#include <fstream>
#include <vector>
#include <algorithm>
void uniq_file(std::istream& in) {
std::string prev;
std::string line;
// first line is always printed to start
getline(in, prev);
fmt::print("{}", prev);
while(in) {
getline(in, line);
if(line != prev) {
fmt::println("{}", line);
prev = line;
}
}
}
int main(int argc, char* argv[]) {
if(argc > 1) {
for(int i = 1; i < argc; i++) {
std::ifstream in_file{argv[i]};
uniq_file(in_file);
}
} else {
uniq_file(std::cin);
}
}
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.