使用CStdioFile 读写UNICODE文档
来源:互联网 发布:淘宝商城招商 编辑:程序博客网 时间:2024/05/21 14:48
一:写文档
1 创建文档并写入内容
CString filePath=L"C:\\unicode.txt";CStdioFile wfile;if (!wfile.Open(filePath,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary)){ AfxMessageBox(L"文件无法修改");return;}WORD sign=0xfeff; // unicode文档 标志wfile.Write(&sign,2);CString fileContent;fileContent=L"UNICODE文档";wfile.Write(fileContent.GetBuffer(),fileContent.GetLength()*2);fileContent.ReleaseBuffer();wfile.Close();
WORD sign=0xfeff;wFile.Write(&sign,2);//遍历list容器 写入文件list<CString>::iterator it=fileList.begin();for (it=fileList.begin();it!=fileList.end();it++){str=*it;//写入CString类型的字符串wFile.Write(str.GetBuffer(),str.GetLength()*2);str.ReleaseBuffer();//使用WriteString 写入回车换行符wFile.WriteString(L"\r\n");}wFile.Close();
2 判断文档是否存在,若存在,则在文档末尾添加内容,若不存在,重新创建
CString filePath=L"c:\\test.txt";CStdioFile wfile;CFileFind fileFind;if(!fileFind.FindFile(filePath)// 查找文件是否存在{if (!wfile.Open(filePath,CFile::modeWrite|CFile::modeCreate|CFile::typeBinary)) //unicode 必须以二进制打开return ;WORD sign=0xfeff;wfile.Write(&sign,2);}else if (!wfile.Open(filePath,CFile::modeWrite|CFile::modeNoTruncate|CFile::typeBinary))return ;WORD sign;wfile.SeekToBegin();wfile.Read(&sign,2);if (sign!=0xfeff){AfxMessageBox(L"不是UNICODE文档");return;}wfile.SeekToEnd();CString str=L"添加到文档末尾";wfile.Write(str.GetBuffer(),str.GetLength()*2);str.ReleaseBuffer();wfile.Close();
二 读文档:
1 使用Read函数 读取文档 ,并将读取到的内容转换为CString 以方便处理
CString filepath; CStdioFile wfile;filepath=L"c:\\test.txt";if (!wfile.Open(filepath,CFile::modeRead|CFile::typeBinary)){AfxMessageBox(L"无法打开文件");return ;}ULONGLONG len=wfile.GetLength();byte *pByte=NULL;if (len>0){pByte=new byte[len];memset(pByte,0,len*sizeof(byte));WORD sign;wfile.SeekToBegin();wfile.Read(&sign,2);if (sign!=0xfeff){AfxMessageBox(L"录入文件不是Unicode");}else{wfile.Read(pByte,len-2);}}else{AfxMessageBox(L"文件长度为0 ");return;}CString buf=(WCHAR*)pByte; // 将数组转换为 CString类型wfile.Close();// 其他操作//...//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 thannMax–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'
因此,若想得到这一行数据,还必须将结尾符去掉方可。
示例:
WORD sign;readFile.SeekToBegin();readFile.Read(&sign,2);if (sign!=0xfeff){AfxMessageBox(L"file.txt 不是Unicode");}else{CString str;//读取文件while (readFile.ReadString(str)) // 此时,获得的str字符串 含有回车符\r 但是不含换行符\n{// 去掉 回车符 \r 获得不含回车换行符的纯粹的字符串数据str=str.Left(str.GetLength()-1);m_filePathMap.SetAt(str,0);}}
一个综合例子:
CFileDialog dlg( TRUE, _T("*"), _T("*.txt"), OFN_FILEMUSTEXIST|OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT, _T("All Files(*.txt|*.txt||)") ); TCHAR szbuffer[1024]; szbuffer[0]=0; dlg.m_ofn.lpstrFile = szbuffer; dlg.m_ofn.nMaxFile = 1024; if(IDOK==dlg.DoModal()) { POSITION pos = dlg.GetStartPosition(); CString filepath; CStdioFile wfile;while(pos!=NULL) { filepath = dlg.GetNextPathName(pos); if (!wfile.Open(filepath,CFile::modeReadWrite|CFile::typeBinary)){AfxMessageBox(L"无法打开文件");return ;}ULONGLONG len=wfile.GetLength();CString strTxt;byte *pByte=NULL;if (len>0){pByte=new byte[len];WORD sign;wfile.SeekToBegin();wfile.Read(&sign,2);if (sign!=0xfeff){AfxMessageBox(L"录入文件不是Unicode");}else{pByte[len-1]=0;pByte[len-2]=0;wfile.Read(pByte,len-2);EraseTxtMark(pByte,len);}}CString buf=(WCHAR*)pByte;wfile.Close();if (!wfile.Open(filepath,CFile::modeReadWrite|CFile::typeBinary|CFile::modeCreate)){AfxMessageBox(L"无法打开文件");return ;}wfile.SeekToBegin();WORD sign=0xfeff;wfile.Write(&sign,2);wfile.Write(buf.GetBuffer(),buf.GetLength()*2);wfile.Close();delete [] pByte;} }
0 0
- 使用CStdioFile 读写UNICODE文档
- 使用CStdioFile 读写UNICODE文档
- 使用CStdioFile 读写UNICODE文档
- 通过CStdioFile读写unicode字符串
- 使用CStdioFile读写文件
- 使用CStdioFile读写文件
- 使用CStdioFile读写文件
- 使用CStdioFile读写文件
- vc中unicode读写文件CStdioFile
- CStdioFile在UNICODE字符集下读写中文
- VC 编程ANSI环境下读写Unicode文件和将CStdioFile类扩展,读取UNICODE文本文件
- VC修改CStdioFile类, 兼容一行一行读写Unicode的和非Unicode的log
- unicode工程中使用CFile 和CStdioFile写入字符串bug
- 使用CStdioFile 读取Unicode文件时出现乱码问题解决方案
- CStdioFile读写文本文件
- MFC读写文件CStdioFile
- CStdioFile读写文件的问题
- MFC读写文件CStdioFile,CFile
- Eclipse在线安装ADT等Android插件连接不上的解决方案
- UVALive - 4329 Ping pong
- HDU 1524 A Chess Game 【SG函数】
- 对类的理解
- 发来的卡夫卡法规和共和国
- 使用CStdioFile 读写UNICODE文档
- 今天第一次写博客,先不说技术了
- nodejs使用express,crypto配合validator实现用户登录逻辑
- c# ThreadPool 应用实例
- 2014华为机试-在字符串中找出连续最长的数字串
- 命名空间
- Split a string using String.split()Tag(s): String/Number
- http://blog.csdn.net/xiaanming/article/details/9401981
- linux 同步时间