CStdioFile读写文件乱码,Unicode读ANSI乱码,需要将ANSI转换为Unicode,再用CFile::typeBinary读写

一:写文档

 

1  创建文档并写入内容

[cpp]  view plain  copy
  1. CString filePath=L"C:\\unicode.txt";  
  2.   
  3. CStdioFile wfile;  
  4. if (!wfile.Open(filePath,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary))  
  5. {   
  6.     AfxMessageBox(L"文件无法修改");  
  7.     return;  
  8. }  
  9.   
  10. WORD sign=0xfeff; // unicode文档 标志  
  11. wfile.Write(&sign,2);  
  12.   
  13. CString fileContent;  
  14. fileContent=L"UNICODE文档";  
  15.   
  16. wfile.Write(fileContent.GetBuffer(),fileContent.GetLength()*2);  
  17. fileContent.ReleaseBuffer();  
  18.   
  19. wfile.Close();  


 

[cpp]  view plain  copy
  1. WORD sign=0xfeff;  
  2. wFile.Write(&sign,2);  
  3.   
  4. //遍历list容器  写入文件  
  5. list<CString>::iterator it=fileList.begin();  
  6. for (it=fileList.begin();it!=fileList.end();it++)  
  7. {  
  8.     str=*it;  
  9.     //写入CString类型的字符串  
  10.     wFile.Write(str.GetBuffer(),str.GetLength()*2);  
  11.     str.ReleaseBuffer();  
  12.   
  13.     //使用WriteString 写入回车换行符  
  14.     wFile.WriteString(L"\r\n");  
  15. }  
  16.   
  17. wFile.Close();  


 

2  判断文档是否存在,若存在,则在文档末尾添加内容,若不存在,重新创建

 

[cpp]  view plain  copy
  1. CString filePath=L"c:\\test.txt";  
  2.           
  3. CStdioFile wfile;  
  4. CFileFind fileFind;  
  5.   
  6. if(!fileFind.FindFile(filePath) // 查找文件是否存在  
  7. {  
  8.     if (!wfile.Open(filePath,CFile::modeWrite|CFile::modeCreate|CFile::typeBinary)) //unicode 必须以二进制打开  
  9.         return ;  
  10.     WORD sign=0xfeff;  
  11.     wfile.Write(&sign,2);  
  12.   
  13. }else    
  14.     if (!wfile.Open(filePath,CFile::modeWrite|CFile::modeNoTruncate|CFile::typeBinary))  
  15.         return ;  
  16.   
  17.   
  18. WORD sign;  
  19.   
  20. wfile.SeekToBegin();  
  21. wfile.Read(&sign,2);  
  22.   
  23. if (sign!=0xfeff)  
  24. {  
  25.     AfxMessageBox(L"不是UNICODE文档");  
  26.     return;  
  27. }  
  28.   
  29. wfile.SeekToEnd();  
  30. CString  str=L"添加到文档末尾";  
  31. wfile.Write(str.GetBuffer(),str.GetLength()*2);  
  32. str.ReleaseBuffer();  
  33. wfile.Close();  


 二 读文档:

1 使用Read函数 读取文档 ,并将读取到的内容转换为CString 以方便处理

[cpp]  view plain  copy
  1. CString    filepath;    
  2. CStdioFile wfile;  
  3.   
  4. filepath=L"c:\\test.txt";  
  5.   
  6. if (!wfile.Open(filepath,CFile::modeRead|CFile::typeBinary))      
  7. {  
  8.     AfxMessageBox(L"无法打开文件");  
  9.     return ;  
  10. }  
  11.   
  12. ULONGLONG len=wfile.GetLength();  
  13. byte *pByte=NULL;  
  14.       
  15. if (len>0)  
  16. {  
  17.     pByte=new byte[len];  
  18.     memset(pByte,0,len*sizeof(byte));  
  19.     WORD sign;  
  20.     wfile.SeekToBegin();  
  21.     wfile.Read(&sign,2);  
  22.   
  23.     if (sign!=0xfeff)  
  24.     {  
  25.         AfxMessageBox(L"录入文件不是Unicode");  
  26.     }else{  
  27.         wfile.Read(pByte,len-2);  
  28.     }  
  29. }else{  
  30.   
  31.     AfxMessageBox(L"文件长度为0 ");  
  32.     return;  
  33. }  
  34.   
  35. CString buf=(WCHAR*)pByte; // 将数组转换为 CString类型  
  36. wfile.Close();  
  37.   
  38. // 其他操作  
  39. //...  
  40. //  
  41.   
  42. delete [] pByte; // 释放资源  


 

2 使用 ReadString  读取一行文本

 

virtual LPTSTR ReadString(
   LPTSTR lpsz,
   UINT nMax 
);
virtual BOOL ReadString(
   CString& rString
);

 

// 读取到换行符\n为止,且读取的字符少于nMax-1时,将换行符 \n 保存到缓冲区中

Reading is stopped by the first newline character. If, in that case, fewer than nMax–1 characters have been read, a newline character is stored in the buffer. A null character ('\0') is appended in either case.

 

注意:

The CString version of this function removes the '\n' if present; the LPTSTR version does not.

 

ReadString(CString & rString)  中, rString   为:  一行文本+结尾符       

但结尾是 回车符 '\r'  而不是'\r\n'  

 

因此,若想得到这一行数据,还必须将结尾符去掉方可。

 

示例:

[cpp]  view plain  copy
  1. WORD sign;  
  2. readFile.SeekToBegin();  
  3. readFile.Read(&sign,2);  
  4. if (sign!=0xfeff)  
  5. {  
  6.     AfxMessageBox(L"file.txt  不是Unicode");  
  7. }else{  
  8.   
  9.     CString str;  
  10.   
  11.     //读取文件  
  12.     while (readFile.ReadString(str))    // 此时,获得的str字符串  含有回车符\r  但是不含换行符\n  
  13.     {  
  14.         // 去掉 回车符 \r  获得不含回车换行符的纯粹的字符串数据  
  15.         str=str.Left(str.GetLength()-1);  
  16.         m_filePathMap.SetAt(str,0);  
  17.     }  
  18. }  

 

 

 

一个综合例子:

 

[cpp]  view plain  copy
  1. CFileDialog  dlg(    
  2.     TRUE,    
  3.     _T("*"),     
  4.     _T("*.txt"),    
  5.     OFN_FILEMUSTEXIST|OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT,     
  6.     _T("All Files(*.txt|*.txt||)")   
  7.     );    
  8. TCHAR    szbuffer[1024];    
  9. szbuffer[0]=0;    
  10. dlg.m_ofn.lpstrFile = szbuffer;    
  11. dlg.m_ofn.nMaxFile = 1024;    
  12.   
  13. if(IDOK==dlg.DoModal())    
  14. {    
  15.     POSITION   pos = dlg.GetStartPosition();    
  16.     CString    filepath;    
  17.     CStdioFile wfile;  
  18.   
  19.     while(pos!=NULL)    
  20.     {    
  21.         filepath = dlg.GetNextPathName(pos);    
  22.   
  23.         if (!wfile.Open(filepath,CFile::modeReadWrite|CFile::typeBinary))     
  24.         {  
  25.             AfxMessageBox(L"无法打开文件");  
  26.             return ;  
  27.         }  
  28.           
  29.         ULONGLONG len=wfile.GetLength();  
  30.   
  31.         CString  strTxt;  
  32.         byte *pByte=NULL;  
  33.   
  34.         if (len>0)  
  35.         {  
  36.             pByte=new byte[len];  
  37.             WORD sign;  
  38.             wfile.SeekToBegin();  
  39.             wfile.Read(&sign,2);  
  40.   
  41.             if (sign!=0xfeff)  
  42.             {  
  43.                 AfxMessageBox(L"录入文件不是Unicode");  
  44.             }else{  
  45.   
  46.                 pByte[len-1]=0;  
  47.                 pByte[len-2]=0;  
  48.                 wfile.Read(pByte,len-2);  
  49.                 EraseTxtMark(pByte,len);  
  50.             }  
  51.         }  
  52.   
  53.   
  54.         CString buf=(WCHAR*)pByte;  
  55.         wfile.Close();  
  56.   
  57.         if (!wfile.Open(filepath,CFile::modeReadWrite|CFile::typeBinary|CFile::modeCreate))   
  58.         {  
  59.             AfxMessageBox(L"无法打开文件");  
  60.             return ;  
  61.         }  
  62.         wfile.SeekToBegin();  
  63.         WORD sign=0xfeff;  
  64.         wfile.Write(&sign,2);  
  65.         wfile.Write(buf.GetBuffer(),buf.GetLength()*2);  
  66.         wfile.Close();  
  67.         delete [] pByte;  
  68.     }    
  69. }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值