-
Notifications
You must be signed in to change notification settings - Fork 961
/
Copy pathcfile-class_10.cpp
58 lines (49 loc) · 1.48 KB
/
cfile-class_10.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//A second example for CFile::Open.
//This function uses CFile to copy binary files.
bool BinaryFileCopy(LPCTSTR pszSource, LPCTSTR pszDest)
{
// constructing these file objects doesn't open them
CFile sourceFile;
CFile destFile;
// we'll use a CFileException object to get error information
CFileException ex;
// open the source file for reading
if (!sourceFile.Open(pszSource,
CFile::modeRead | CFile::shareDenyWrite, &ex))
{
// complain if an error happened
// no need to delete the ex object
TCHAR szError[1024];
ex.GetErrorMessage(szError, 1024);
_tprintf_s(_T("Couldn't open source file: %1024s"), szError);
return false;
}
else
{
if (!destFile.Open(pszDest, CFile::modeWrite |
CFile::shareExclusive | CFile::modeCreate, &ex))
{
TCHAR szError[1024];
ex.GetErrorMessage(szError, 1024);
_tprintf_s(_T("Couldn't open source file: %1024s"), szError);
sourceFile.Close();
return false;
}
BYTE buffer[4096];
DWORD dwRead;
// Read in 4096-byte blocks,
// remember how many bytes were actually read,
// and try to write that many out. This loop ends
// when there are no more bytes to read.
do
{
dwRead = sourceFile.Read(buffer, 4096);
destFile.Write(buffer, dwRead);
}
while (dwRead > 0);
// Close both files
destFile.Close();
sourceFile.Close();
}
return true;
}