最近因为用evc开发手机上的程序,遇到了一个小难题,就是evc关于文本的读写问题,因为evc用的是unicode,而CFile中的read和write函数是非unicode的,所以读写都需要进行转换。但是如何转换是一个大问题。 在网上也搜索了一下,读入的问题有答案,但写的问题却好像没有什么好的办法,经过一番波折和尝试后终于解决了这个问题。下面是读入和写出的示例代码(希望对各位同仁有帮助):(2008/11/05 23:32 重新编辑)
- void CMLogDoc::SaveNetConfig()
- {
- CString strFileName="net.txt";
- CFile f(strFileName,CFile::modeCreate|CFile::modeWrite);
- CString strReturn=_T("/r/n");
- CString str=_T("hi/r/nok");
- DWORD dwLen=str.GetLength();
- char *pBuf=new char[dwLen];
- for(int i=0;i<dwLen;i++)//unicode转化为非unicode
- {
- TCHAR tch=str.GetAt(i);
- pBuf[i]=(char)tch;
- }
- pBuf[dwLen]='/0';
- f.Write(pBuf,dwLen+1);
- f.Close();
- delete pBuf;
- }
- void CMLogDoc::ReadNetConfig()
- {
- WIN32_FIND_DATA wfd;
- CString strFileName=GetAppPath()+"config//net.txt";
- if(FindFirstFile(strFileName,&wfd)==INVALID_HANDLE_VALUE ) return;
- CFile f(strFileName,CFile::modeRead);
- DWORD dwLen=f.GetLength();
- char *pBuf=new char[dwLen+1];
- UINT nBytesRead=f.Read(pBuf,dwLen);
- pBuf[dwLen]='/0';
- f.Close();
- CString str=pBuf;//pbuf中的ansi字符会被自动转换成unicode。
- delete pBuf;
- }