//Eric Diamond //CSE 660 //Programming Assignment #1 //File Copying //****************************************** #include #include #include #include #include using namespace std; void arg(int argc, char *argv[]); char *fout_filename; char *fin_filename; bool use_file_out = false; bool use_screen_out = true; //****************************************** int main(int argc, char *argv[]) { //declarations and stuff arg(argc, argv); FILE * inFile; FILE * outFile; int x = 0; inFile=fopen(fin_filename, "r"); outFile=fopen(fout_filename,"wt"); //check in and output files if (!inFile) { cout << "Unable to open input file" << endl; exit(1); // terminate with error } if (!outFile) { cout << "Unable to open output file" << endl; exit(1); // terminate with error } //basic menu loop do { cout<> x; }while(x>4 || x<0); //switch between 4 different copy types switch (x) { case 1: //getc and putc { char c; while ((c = getc(inFile)) != EOF) //put chars in c untill end of file { putc(c , outFile); // write c to output } break; } case 2: //read and write { size_t num; char buffer[BUFSIZ]; while((num = read(fileno(inFile), buffer, BUFSIZ)) > 0) //fill buffer as long as still stuff in file { if(num!= write(fileno(outFile), buffer, num)) //write buffer { cout << "Error!!!!!"; exit(1); } } break; } case 3: //fread and fwrite { size_t num; char buffer[BUFSIZ]; //fread(void *ptr, size_t size, size_t nitems, FILE *stream) while((num = fread(buffer, sizeof(buffer[0]), BUFSIZ, inFile)) > 0) { //size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream) if(num!= fwrite(buffer, sizeof(buffer[0]), num, outFile)) { cout << "Error!!!!!"; exit(1); } } break; } case 4: //fseek and getc putc { lseek(fileno(outFile), 214, SEEK_SET); // goto position 214 and write data starting there char c; while ((c = getc(inFile)) != EOF) //same as getc putc above { putc(c , outFile); } break; break; } default: { cout << "That was not a valid option!" << endl; break; } } //close files fclose(inFile); fclose(outFile); return 0; } //****************************************** //parse command line args void arg(int argc, char *argv[]) { for (int i = 1; i < argc; i++) { if (strcmp("-i", argv[i]) == 0) // input file { fin_filename = argv[++i]; cerr << "Input file: " << fin_filename << endl; } if (strcmp("-o", argv[i]) == 0) // output file { use_file_out = true; use_screen_out = false; fout_filename = argv[++i]; cerr << "Output file: " << fout_filename << endl; } } }