Getting closer...
Still not working right though.
I am here...
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <iterator>
#include <algorithm>
using namespace std;
string avix(unsigned char);
int main (int argc, char** argv) {
streambuf* inbuf = cin.rdbuf();
streambuf* outbuf = cout.rdbuf();
cin.rdbuf(inbuf);
cout.rdbuf(outbuf);
while (argc > 1) {
if (argc % 2 == 0 || argc > 5) {
cerr << "Wrong number of arguments\n";
exit(1);
}
if ( ( argc > 1) && ( !strcmp (argv[argc-2], "-i") ) ) {
ifstream inFile (argv[argc-1], ios::in);
if (!inFile.is_open()) {
cerr << "Could not open file " << argv[argc-1] << " for reading!\n";
exit(1);
}
argc -= 2;
cin.rdbuf(inFile.rdbuf());
}
if ( ( argc > 1) && ( !strcmp (argv[argc-2], "-o") ) ) {
ofstream outFile (argv[argc-1], ios::app);
if (!outFile.is_open()) {
cerr << "Could not open file for " << argv[argc-1] << " writing!\n";
exit(1);
}
argc -= 2;
cout.rdbuf(outFile.rdbuf());
}
}
transform(istream_iterator<unsigned char>(cin),istream_iterator<unsigned char>(),ostream_iterator<string>(cout),avix);
cout << '\n';
cout.rdbuf(outbuf);
cin.rdbuf(inbuf);
return 0;
}
With no arguments it works properly, and if I remove the argument handling and cin and cout and go straight from file to file it works, reading chars, translating them through avix() and then outputting them to a file or cout.
But this way I either get a segmentation fault or no effect, it is has any args. (But calling it without args works perfectly)
On a related note, does anyone know how to send EOF down cin? Or is there an end iterator for an input stream that I am overlooking? On files that transform() worked fine (with an fstream) but when input comes from cin it keeps taking more and more characters until I shut it down from outside.