Skip to content

Instantly share code, notes, and snippets.

@ajpen
Created June 26, 2015 16:16
Show Gist options
  • Save ajpen/54d0bb1e0a33017f3814 to your computer and use it in GitHub Desktop.
Save ajpen/54d0bb1e0a33017f3814 to your computer and use it in GitHub Desktop.
/* ---------------------------------------
* File: filecpy.cpp
* Usage: ./filecpy source destination
* ---------------------------------------
* Function: Copies the source file to
* the new file specified by the path
* called destination. The program
* creates the new file and then copies
* the contents of source to it.
* _______________________________________
*/
/* INCLUDES */
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
/* NAMESPACE FUNCTIONS */
// namespace to allow "free" usage of cout
using std::cout;
//main
int main(int argc, char const *argv[]) {
// character for reading and writing
char c;
// first determine that the file paths are usable
ifstream source;
ofstream dest;
// opens the source file and checks for errors
source.open(argv[1]);
if (source.fail()) {
cout << "FATAL: Could not open source file." << endl;
return 1;
}
// opens dest file and checks for errors
dest.open(argv[2]);
if (dest.fail()) {
cout << "FATAL: Could not open destination file." << endl;
return 2;
}
// start reading characters and writing them to dest
while (!source.fail() && source.get(c)) {
dest << c;
}
// closes files
source.close();
dest.close();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment