我在VS2005上编写了一个基于MFC的文件载入、保存函数,该函数在PC端正常运行但是在WinCE6平台上不能运行。我猜想应该是与ASCII编码和Unicode编码有关。
如果载入ASCII编码的文本文件,如果Open函数中含有CFile::typeText,则会报错;如果屏蔽这点,会载入乱码。我想这可能是因为ASCII文本由Unicode解码产生。我想问为什么WinCE中不能使用CFile::typeText,如果想在WinCE6中读取或写出ASCII文本文件,应该如何进行。
单步调试到 filecore.cpp 文件的// text mode not supported。
BOOL CFile::Open(LPCTSTR lpszFileName, UINT nOpenFlags,
CFileException* pException)
{
ASSERT_VALID(this);
ASSERT(AfxIsValidString(lpszFileName));
ASSERT(pException == NULL ||
AfxIsValidAddress(pException, sizeof(CFileException)));
ASSERT((nOpenFlags & typeText) == 0); // text mode not supported
// shouldn't open an already open file (it will leak)
ASSERT(m_hFile == INVALID_HANDLE_VALUE);
// CFile objects are always binary and CreateFile does not need flag
nOpenFlags &= ~(UINT)typeBinary;
m_bCloseOnDelete = FALSE;
m_hFile = INVALID_HANDLE_VALUE;
m_strFileName.Empty();
------解决思路----------------------
CFile::typeText Sets text mode with special processing for carriage return–linefeed pairs (used in derived classes only).
CFile::typeText 的 MSDN 中这样描述:仅在派生类中使用
偶对 CFile 不熟悉,所以一般都直接使用 FILE 来完成对文件的操作。
使用 FILE 读取,需要自己根据文件的前两个字节判断文件的编码类型,是 ASCII、还是 Unicode,然后按编码类型进行处理。
------解决思路----------------------
typeText = (int) 0x04000, // typeText and typeBinary are
typeBinary = (int) 0x08000, // used in derived classes only
上面是头文件里面说明,说清楚了。
读ASCIII编码的文本文件肯定是好用的,我项目里一直用,不需要加typeText 属性。
CFile 封装了ReadFile,WriteFile,CreateFile等API,不支持直接读Unicode的,要的话要自己读取后转码。
应该是你的代码有问题,wince部分不支持。