os-70-350

os-70-350 Commit Details


Date:2013-11-24 21:37:24 (11 years 9 months ago)
Author:Natalie Adams
Branch:master
Commit:8ad0be73a293df1811e33513c5c8b9113092ccec
Parents: a4f6de85241985b5e171b9e64383b3198a2f023b
Message:updating code

Changes:

File differences

cp/Debug/test.txt
1
1
this is a test
this is a test1234
cp/Debug/test2.txt
1
this is a test1234
cp/cp/main.cpp
22
33
44
5
56
67
78
89
9
10
11
12
13
14
10
11
12
1513
1614
15
16
17
18
19
20
21
22
23
24
25
1726
1827
19
20
28
29
2130
2231
32
2333
2434
25
35
36
37
2638
2739
2840
2941
3042
3143
44
45
46
47
48
49
3250
51
52
53
54
55
56
57
58
59
60
61
3362
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;
}

Archive Download the corresponding diff file

Branches

Number of commits:
Page rendered in 0.53451s using 14 queries.