#include #include // procedure to get a files size and handles large files long long GetFileSizeLocal(const TCHAR *fName) { BOOL Ok; WIN32_FILE_ATTRIBUTE_DATA fInfo; if (NULL == fName) return (long long) -1; Ok = GetFileAttributesEx(fName, GetFileExInfoStandard, (void*)&fInfo); if (!Ok) return (long long) -1; return (long long)fInfo.nFileSizeHigh * 0x100000000 + fInfo.nFileSizeLow; } int main() { FILE *fptr1, *fptr2; // File Handles char filename1[100], filename2[100]; // File Names long long pos = 0; // File Position long long rlen; // Record Length to read and write long long sz1, sz2; // Size of files long long bufsize = (1024 * 1024); // 1 MB Buffer size char buf[bufsize]; // 1 Megabyte buffer long long lenread; // bytes actually read long long lenwrite; // bytes actually written char check; // read keyboard character printf("Enter the filename to open for reading \n"); scanf("%s", filename1); // Open one file for reading fptr1 = fopen(filename1, "rb"); if (fptr1 == NULL) { printf("Cannot open file %s \n", filename1); // Exit with first file error return 1; } sz1 = GetFileSizeLocal((TCHAR*)filename1); printf("File Size #1 is %lld.\n", sz1); fclose(fptr1); printf("Enter the filename to open for writing \n"); scanf("%s", filename2); // // See if File Exists and if so display warning fptr2 = fopen(filename2, "r"); if (fptr2 != NULL) { fclose(fptr2); printf("File '%s' Already Exists. Do you want to Overwrite it?\n", filename2); check = getch(); if (check == 110 || check == 78) // "N" or "n" // Exit with second file error return 2; } // // Open second file for writing fptr2 = fopen(filename2, "wb"); if (fptr2 == NULL) { printf("Cannot open file %s \n", filename2); // Exit with second file error return 2; } sz2 = GetFileSizeLocal(filename2); fclose(fptr2); // Set up output line that we can backspace over printf("\nBytes Copied: %-12lld", 0); // Start of loop while (pos < sz1) { // Open first file for reading fptr1 = fopen(filename1, "rb"); sz2 = GetFileSizeLocal(filename2); // Open second file for writing fptr2 = fopen(filename2, "ab"); // get file #2 size which changes here sz2 = GetFileSizeLocal(filename2); // check if we need a full bufsize or less bytes to handle if ((sz1 - sz2) > bufsize) rlen = bufsize; else rlen = sz1 - sz2; // Set position of first file to read fseek(fptr1, pos, SEEK_SET); // Read bytes lenread = fread(buf, 1, rlen, fptr1); // Check for read errors and show error message and exit if (lenread != rlen) { printf("Read Error Occurred. Copy not completed!\n"); fclose(fptr1); fclose(fptr2); // Exit with first file error return 1; } // Set position of second file to write fseek(fptr2, pos, SEEK_SET); // Write bytes lenwrite = fwrite(buf, 1, lenread, fptr2); // Check for write errors and show error message and exit if (lenread != lenwrite) { printf("Write Error Occurred. Copy not completed!\n"); fclose(fptr1); fclose(fptr2); // Exit with second file error return 2; } // Close first file which is being read fclose(fptr1); // Close second file which is being written fclose(fptr2); // calculation the new file position where we are working pos = pos + bufsize; // Show progress on command line using same line and not scrolling printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\bBytes Copied: %-12lld", pos); // End of loop here } // Show Finished Message printf("\nContents copied to %s", filename2); // Close First file used for reading fclose(fptr1); // Close Second file used for writing fclose(fptr2); // Exit with no error return 0; }