diff --git a/cp/Debug/test.txt b/cp/Debug/test.txt index a8a9406..7cb95e5 100644 --- a/cp/Debug/test.txt +++ b/cp/Debug/test.txt @@ -1 +1 @@ -this is a test \ No newline at end of file +this is a test1234 \ No newline at end of file diff --git a/cp/Debug/test2.txt b/cp/Debug/test2.txt new file mode 100644 index 0000000..7cb95e5 --- /dev/null +++ b/cp/Debug/test2.txt @@ -0,0 +1 @@ +this is a test1234 \ No newline at end of file diff --git a/cp/cp/main.cpp b/cp/cp/main.cpp index ccd43d5..765340e 100644 --- a/cp/cp/main.cpp +++ b/cp/cp/main.cpp @@ -2,32 +2,61 @@ void WriteStr(const char * str) { + // A simple wrapper to output text using WriteFile HANDLE stdout2 = GetStdHandle(STD_OUTPUT_HANDLE); DWORD byteswritten; int length = 0; const char * cpyptr = str; - while(*cpyptr != 0) - { - cpyptr++; - length += 1; - } - WriteFile(stdout2, str, sizeof(str), &byteswritten, NULL); + while(*cpyptr++) + length++; + WriteFile(stdout2, str, length, &byteswritten, NULL); } +class FileWrapper +{ +private: + HANDLE _handle; + +public: + FileWrapper(const char * file, DWORD access, DWORD creation) { this->_handle = CreateFileA(file, access, 0, NULL, creation, FILE_ATTRIBUTE_NORMAL, NULL); } + ~FileWrapper() { CloseHandle(this->_handle); } + HANDLE getHandle() { return this->_handle; } +}; + int main(int argc, char * args[]) { - WriteStr("abc"); - if (argc != 2) + // Parameter checking + if (argc != 3) { WriteStr("Please enter two parameters - cp src dst"); + return 0; } - HANDLE hsrc = CreateFileA(args[1], GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + // Open the source + // A little RAII + FileWrapper src(args[1], GENERIC_READ, OPEN_EXISTING); if (GetLastError() == ERROR_FILE_NOT_FOUND) { WriteStr("Could not find file!"); return 0; } + // Open destination file + FileWrapper dst(args[2], GENERIC_WRITE, CREATE_ALWAYS); + + DWORD numberofbytesread; + DWORD numberofbyteswritten; + char buffer[20]; + // Stream the file instead of reading it into memory + while(true) + { + // The buffer parameter is a void pointer which means it can take anything - in this case we just use a char array + ReadFile(src.getHandle(), buffer, 20, &numberofbytesread, NULL); + if (numberofbytesread == 0) + break; + WriteFile(dst.getHandle(), buffer, numberofbytesread, &numberofbyteswritten, NULL); + } + + return 0; } \ No newline at end of file