// compile with: g++ hello.cpp
#include<iostream>
#include<fstream>
#include<stdlib.h>
using namespace std;
int main(int argc, char * argv[]) {
if (argc < 3) {
cout << "Usage: ./files srcFile destFile\n";
exit(0);
}
// open input file
// note: this line calls a constructor for in that takes a C string as a parameter
// constructor initializes some things in the variable
ifstream in(argv[1]); // somewhere in there it does something like fopen or open
// check that file was opened
// good is a method of ifstream
if (! in.good()) {
cout << "Unable to open " << argv[1] << " for reading." << endl;
exit(0);
}
// open output file
ofstream out(argv[2]);
// check that file was opened
if (! out.good()) {
cout << "Unable to open " << argv[2] << " for writing." << endl;
exit(0);
}
// loop to read from input and write to output
char line[1000];
while (in.getline(line, 999)) {
out << line << endl;
}
out.close();
in.close();
return 0;
}