wzpan
10/3/2013 - 9:11 AM

C++ - file io

C++ - file io

#include <iostream>
#include <fstream>
#include <sstream>
#include <stdexcept>

using namespace std;

int main(int argc, char *argv[])
{
    if(argc != 3){
        string err_info;
        ostringstream oss(err_info);
        oss << argv[0] << " dir1/file1 dir2/file2" << endl;
        cerr << oss.str();
        return -1;
    }

    string in = argv[1];
    string out = argv[2];
    string content;

    ifstream ifs(in.c_str());
    ofstream ofs(out.c_str());

    if (!ifs) { 
        cerr << "error: unable to open input file: " 
             << in << endl; 
        return -1; 
    }
    if (!ofs) { 
        cerr << "error: unable to open output file: " 
             << out << endl; 
        return -1; 
    }     

    while(getline(ifs, content)){
        ofs << content << endl;
    }
}