Simple Text Processing With C++ dos2unix Example

March 24, 2014

This is a very basic example of a dos2unix application. It demonstrates using std::remove_copy() and stream iterators to process streams from an input stream to an output stream.

#include <fstream>
#include <iostream>
#include <iterator>
#include <algorithm>
 
using namespace std;
 
int main(int argc, char** argv)
{
   string filename = "/dev/stdin";
 
   if (argc > 1)
   {
      filename = argv[1];
   }
 
   std::ifstream is(filename.c_str());
   if (!is.is_open())
   {
      cerr << "error: could not open " << filename << endl;
      return 1;
   }
   std::ofstream os("/dev/stdout");
   std::istreambuf_iterator<char> in(is), end;
   std::ostreambuf_iterator<char> out(os);
   remove_copy(in, end, out, '\r');
   return 0;
}

Related Posts