// Copy a file #include // std::ifstream, std::ofstream #include #include using namespace std; int main (int argc, char *argv[]) { ofstream outfile(argv[argc-1], ios::binary | ios::trunc); if (!outfile) { cerr << "Bad file open for writing"; exit(1); } // Copy each file from input to output for(int i = 1; i < argc-1; i++) { ifstream infile(argv[i], ios::binary); if (!infile) { cerr << "Bad file open!" << endl; exit(1); } // get size of file infile.seekg (0,infile.end); long size = infile.tellg(); infile.seekg (0); // allocate memory for file content char* buffer = new char[size]; // read content of infile infile.read (buffer,size); // write to outfile outfile.write (buffer,size); // release dynamically-allocated memory delete[] buffer; if (infile && outfile) { cout << "Wrote " << size << " Bytes from " << argv[i] << "to " << argv[argc-1] << endl; } else { cerr << "Error writing!" << strerror(errno) << endl; } infile.close(); } outfile.close(); return 0; }