//A second example for CFile::Open.
//This console program uses CFile to copy binary files.
#include <afx.h>
#include <afxwin.h>
#include <iostream>
using namespace std;
CWinApp theApp;
int main(int argc, char *argv[])
{
if (!AfxWinInit(GetModuleHandle(NULL), NULL, GetCommandLine(), 0))
{
cout << "panic: MFC couldn't initialize!" << endl;
return 1;
}
// constructing these file objects doesn't open them
CFile sourceFile;
CFile destFile;
// see that we have a reasonable number of arguments
if (argc != 3)
{
cout << "usage: " << argv[0];
cout << " <source> <dest>" << endl;
cout << endl;
return 1;
}
// we'll use a CFileException object to get error information
CFileException ex;
// open the source file for reading
if (!sourceFile.Open(argv[1], CFile::modeRead | CFile::shareDenyWrite, &ex))
{
// complain if an error happened
// no need to delete the ex object
TCHAR szError[1024];
ex.GetErrorMessage(szError, 1024);
cout << "Couldn't open source file: ";
cout << szError;
return 1;
}
else
{
if (!destFile.Open(argv[2], CFile::modeWrite | CFile::shareExclusive | CFile::modeCreate, &ex))
{
TCHAR szError[1024];
ex.GetErrorMessage(szError, 1024);
cout << "Couldn't open source file: ";
cout << szError;
sourceFile.Close();
return 1;
}
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 0;
}
[code]
nOpenFlags
Sharing and access mode. Specifies the action to take when opening the file. You can combine options listed below by using the bitwise-OR (|) operator. One access permission and one share option are required; the modeCreate and modeNoInherit modes are optional. The values are as follows:
[/code]
CFile::modeCreate Directs the constructor to create a new file. If the file exists already, it is truncated to 0 length.
CFile::modeNoTruncate Combine this value with modeCreate. If the file being created already exists, it is not truncated to 0 length. Thus the file is guaranteed to open, either as a newly created file or as an existing file. This might be useful, for example, when opening a settings file that may or may not exist already. This option applies to CStdioFile as well.
CFile::modeRead Opens the file for reading only.
CFile::modeReadWrite Opens the file for reading and writing.
CFile::modeWrite Opens the file for writing only.
CFile::modeNoInherit Prevents the file from being inherited by child processes.
CFile::shareDenyNone Opens the file without denying other processes read or write access to the file. Create fails if the file has been opened in compatibility mode by any other process.
CFile::shareDenyRead Opens the file and denies other processes read access to the file. Create fails if the file has been opened in compatibility mode or for read access by any other process.
CFile::shareDenyWrite Opens the file and denies other processes write access to the file. Create fails if the file has been opened in compatibility mode or for write access by any other process.
CFile::shareExclusive Opens the file with exclusive mode, denying other processes both read and write access to the file. Construction fails if the file has been opened in any other mode for read or write access, even by the current process.
CFile::shareCompat This flag is not available in 32 bit MFC. This flag maps to CFile::shareExclusive when used in CFile::Open.
CFile::typeText Sets text mode with special processing for carriage return–linefeed pairs (used in derived classes only).
CFile::typeBinary Sets binary mode (used in derived classes only).
CFile::osNoBuffer The system opens a file with no system caching. For more information see FILE_FLAG_NO_BUFFERING in CreateFile in the Platform SDK.
CFile::osWriteThrough The system writes through any intermediate cache and goes directly to disk. For more information see FILE_FLAG_WRITE_THROUGH in CreateFile in the Platform SDK.
CFile::osRandomAccess A file is accessed randomly. The system can use this as a hint to optimize file caching.
CFile::osSequentialScan A file is accessed sequentially from beginning to end. The system can use this as a hint to optimize file caching. For more information see FILE_FLAG_SEQUENTIAL_SCAN in CreateFile in the Platform SDK.
//This console program uses CFile to copy binary files.
#include <afx.h>
#include <afxwin.h>
#include <iostream>
using namespace std;
CWinApp theApp;
int main(int argc, char *argv[])
{
if (!AfxWinInit(GetModuleHandle(NULL), NULL, GetCommandLine(), 0))
{
cout << "panic: MFC couldn't initialize!" << endl;
return 1;
}
// constructing these file objects doesn't open them
CFile sourceFile;
CFile destFile;
// see that we have a reasonable number of arguments
if (argc != 3)
{
cout << "usage: " << argv[0];
cout << " <source> <dest>" << endl;
cout << endl;
return 1;
}
// we'll use a CFileException object to get error information
CFileException ex;
// open the source file for reading
if (!sourceFile.Open(argv[1], CFile::modeRead | CFile::shareDenyWrite, &ex))
{
// complain if an error happened
// no need to delete the ex object
TCHAR szError[1024];
ex.GetErrorMessage(szError, 1024);
cout << "Couldn't open source file: ";
cout << szError;
return 1;
}
else
{
if (!destFile.Open(argv[2], CFile::modeWrite | CFile::shareExclusive | CFile::modeCreate, &ex))
{
TCHAR szError[1024];
ex.GetErrorMessage(szError, 1024);
cout << "Couldn't open source file: ";
cout << szError;
sourceFile.Close();
return 1;
}
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 0;
}
[code]
nOpenFlags
Sharing and access mode. Specifies the action to take when opening the file. You can combine options listed below by using the bitwise-OR (|) operator. One access permission and one share option are required; the modeCreate and modeNoInherit modes are optional. The values are as follows:
[/code]
CFile::modeCreate Directs the constructor to create a new file. If the file exists already, it is truncated to 0 length.
CFile::modeNoTruncate Combine this value with modeCreate. If the file being created already exists, it is not truncated to 0 length. Thus the file is guaranteed to open, either as a newly created file or as an existing file. This might be useful, for example, when opening a settings file that may or may not exist already. This option applies to CStdioFile as well.
CFile::modeRead Opens the file for reading only.
CFile::modeReadWrite Opens the file for reading and writing.
CFile::modeWrite Opens the file for writing only.
CFile::modeNoInherit Prevents the file from being inherited by child processes.
CFile::shareDenyNone Opens the file without denying other processes read or write access to the file. Create fails if the file has been opened in compatibility mode by any other process.
CFile::shareDenyRead Opens the file and denies other processes read access to the file. Create fails if the file has been opened in compatibility mode or for read access by any other process.
CFile::shareDenyWrite Opens the file and denies other processes write access to the file. Create fails if the file has been opened in compatibility mode or for write access by any other process.
CFile::shareExclusive Opens the file with exclusive mode, denying other processes both read and write access to the file. Construction fails if the file has been opened in any other mode for read or write access, even by the current process.
CFile::shareCompat This flag is not available in 32 bit MFC. This flag maps to CFile::shareExclusive when used in CFile::Open.
CFile::typeText Sets text mode with special processing for carriage return–linefeed pairs (used in derived classes only).
CFile::typeBinary Sets binary mode (used in derived classes only).
CFile::osNoBuffer The system opens a file with no system caching. For more information see FILE_FLAG_NO_BUFFERING in CreateFile in the Platform SDK.
CFile::osWriteThrough The system writes through any intermediate cache and goes directly to disk. For more information see FILE_FLAG_WRITE_THROUGH in CreateFile in the Platform SDK.
CFile::osRandomAccess A file is accessed randomly. The system can use this as a hint to optimize file caching.
CFile::osSequentialScan A file is accessed sequentially from beginning to end. The system can use this as a hint to optimize file caching. For more information see FILE_FLAG_SEQUENTIAL_SCAN in CreateFile in the Platform SDK.
'Windows > MFC' 카테고리의 다른 글
XML Parser (0) | 2013.10.02 |
---|---|
MSXML 6.0 (0) | 2013.10.02 |
XMLite: simple XML parser. (0) | 2013.10.02 |
XML using MFC (0) | 2013.10.02 |
Exception class (0) | 2013.10.02 |