CFIIE类是MFC的文件类的基类,它直接提供无缓冲的二进制磁盘I/O设备,并且通过它的派生类可以提供对text文件和内存文件的存取。CFILE与CArchive类一起提供对MFC序列化的支持。
CFILE类和它的派生类之间的等级关系,允许你的程序通过多态的CFILE界面对所有文件类进行操作。比如内存文件,或者磁盘文件以及类似的。
CFILE和它的派生类用于执行一般的磁盘I/O操作。而OFSTREAM或者其它的微软 iostream类则是用于把格式化text输出到磁盘文件上去。
通常,CFILE类的构造过程中会自动打开磁盘文件,在析构函数中则会自动关闭文件。你可以使用静态成员函数来询问文件的状态而不用打开文件。
使用CFILE类必须包含 #include
virtual BOOL Open(
LPCTSTR lpszFileName,
UINT nOpenFlags,
CFileException* pError = NULL
);
virtual BOOL Open(
LPCTSTR lpszFileName,
UINT nOpenFlags,
CAtlTransactionManager* pTM,
CFileException* pError = NULL
);
lpszFileName 需要打开文件的路径字符串,这个路径可以是相对路径也可以是绝对路径,或者是网络名字(UNC)
nOpenFlags 一个UINT定义文件的存取共享模式。它指定文件打开时可以采取的操作。你可以使用”|"号来组合多个选项。文件的一个存取权限和一个共享选项是必须要指定的。而modeCreate 和modeNoInherit是可选的。
具体如下:
下面的五个表列出了 nOpenFlags 参数可能的选项。
仅选择下列文件访问模式选项之一。 默认文件访问模式为 CFile::modeRead,该模式为只读模式。
值 |
描述 |
---|---|
CFile::modeRead |
只请求读取访问权限。 |
CFile::modeWrite |
只请求写入访问权限。 |
CFile::modeReadWrite |
请求读写访问权限。 |
选择以下字符模式选项之一。
值 |
描述 |
---|---|
CFile::typeBinary |
设置二元模式(仅在派生类中使用)。 |
CFile::typeText |
通过对回车-换行对进行特殊处理,来设置文本模式(仅在派生类中使用)。 |
CFile::typeUnicode |
设置 Unicode 模式(仅在派生类中使用)。 当应用程序在 Unicode 配置中生成时,文本将以 Unicode 格式写入文件中。 不会将 BOM 写入该文件中。 |
仅选择下列文件共享模式选项之一。 默认文件共享模式为 CFile::shareExclusive,该模式是独占模式。
值 |
描述 |
---|---|
CFile::shareDenyNone |
没有任何共享限制。 |
CFile::shareDenyRead |
拒绝向所有其他用户提供读取访问权限。 |
CFile::shareDenyWrite |
拒绝向所有其他用户提供写入访问权限。 |
CFile::shareExclusive |
拒绝向所有其他用户提供读写访问权限。 |
选择下面的第一个(或全选)文件创建模式选项。 默认创建模式为 CFile::modeNoTruncate,该模式当前处于打开状态。
值 |
描述 |
---|---|
CFile::modeCreate |
若文件不存在,则创建新文件;若该文件已存在,则引发 CFileException。 |
CFile::modeNoTruncate |
若文件不存在,则创建新文件;否则,如果该文件已存在,则将其附加到 CFile 对象。 |
按照描述选择以下文件缓存选项。 默认情况下,系统将使用通用的缓存方案,该方案不可用作选项。
值 |
描述 |
---|---|
CFile::osNoBuffer |
系统没有为文件使用中间缓存。 此选项取消以下 2 个选项。 |
CFile::osRandomAccess |
文件缓存针对随机访问进行了优化。 不要使用此选项和顺序扫描选项。 |
CFile::osSequentialScan |
文件缓存针对顺序访问进行了优化。 不要使用此选项和随机访问选项。 |
CFile::osWriteThrough |
立即执行写入操作。 |
选择下列安全选项以防止继承文件句柄。 默认情况下,所有新的子进程都可以使用文件句柄。
值 |
描述 |
---|---|
CFile::modeNoInherit |
阻止任何子进程使用文件句柄。 |
pError :要接收失败的操作状态的现有文件异常对象的指针。
pTM :为CAtlTransactionManager对象的指针
返回值:
非零,则打开成功;否则为0。 才返回0,pError 参数是有意义的。
CFile f;
CFileException e;
TCHAR* pszFileName = _T("Open_File.dat");
if(!f.Open(pszFileName, CFile::modeCreate | CFile::modeWrite, &e))
{
TRACE(_T("File could not be opened %d\n"), e.m_cause);
}
//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;
}