1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
#include <fcntl.h>
#include <unistd.h>
#include <cstdlib>
#include <iostream>
using namespace std; //namespace
const int bufsiz = 4096; //blah blah...
int main(int argc, char *argv[]){
int fin, fout, rc; //create the ints
char buffer[bufsiz]; //create a char* called buffer that is bufsiz in length
if(argc != 3) { //if there are not 3 arguments (argc means "arg count")
cerr << "need 2 filenames\n"; exit(1); //display error/exit
}
fin=open(argv[1],O_RDONLY); //open the file described in argv[1] (argument 1) with readonly access and store it to fin
if(fin < 0) { //if fin < 0 (due to an error in open I assume)
cerr << "cannot open " << argv[1] << "\n"; exit(1 //display error, exit
}
fout=open(argv[2],O_WRONLY|O_TRUNC|O_CREAT,0644); //open the new file for read/write, create it if it doesn't exist, and delete stuff it in if it has stuff in it
if(fout < 0) { //if it errored
cerr << "cannot open " << argv[2] << "\n"; exit(1);
}
rc = read(fin, buffer, bufsiz); //read data into char* (as much as possible)
while(rc > 0) { //as long as it is getting data
write(fout, buffer, rc); //write data to the file
rc = read(fin, buffer, bufsiz); //read more
}
close(fout); //close the file
//implied return 0;
}
|