赞
踩
把CString中的内容写到UTF16LE中去,可以使用WriteString或者Write。
WriteString函数会把UNICODE字符串以UTF16LE编码格式写入,遇到空字符会提前结束
Write函数则不受空字符的影响,不会提前结束。
使用WriteString向UTF16LE中写入CString类型的文本。
- CStdioFileCodePage file1;
- if (!file1.OpenWithoutType(CppFile, CFile::modeRead))
- {
- file1.Close();
- return false;
- }
- CStdioFile tempFile;
- CString tempFileName = CppFile + L".tmp";
-
-
-
- if (!tempFile.Open(tempFileName, CFile::modeCreate | CFile::modeWrite | CFile::typeUnicode))
- {
- tempFile.Close();
- return false;
- }
- CString line;
-
- while (file1.ReadStringWithOutType(line))
- {
-
- if (line.Compare(msg3) != 0)
- {
- tempFile.WriteString(line + L"\n");
- }
- else
- {
- break;
- }
- }
-
- tempFile.Close();
- file1.Close();
-
- CFile::Remove(CppFile);
- CFile::Rename(tempFileName, CppFile);
-
- CStdioFile file;
- if (!file.Open(CppFile, CFile::modeWrite | CFile::typeUnicode))
- {
- file.Close();
- return false;
- }
- file.SeekToEnd();
- file.WriteString(finamsg);
-
-
- file.Close();
- return true;
WriteString在写入文本内容后可以指针定位在文件末尾追加
Write再写入后如果没有包含CFile::modeNoTruncate模式则会选择覆盖原本内容
使用Write函数向UTF16LE中写入CString内容。
file.Write(finamsg, finamsg.GetLength() * sizeof(wchar_t));
内容如果写的不完全注意:UTF16是一个字符由俩个字节表示,所以在第二个参数*2.
UTF16BE和UTF16LE的区别
1)、UTF16BE是大端序存储,也就是开头有大端BOM,UTF16LE是小端序存储,也就是开头有小端BOM。
2)、由于MFC中默认不支持自动添加大端序BOM所以在创建UTF16BE的时候要使用CFile::typeBinary模式来手动处理字节顺序,确保文件以大端字节顺序存储Unicode字符.而创建小端序可以直接使用UNICODE默认的方式创建。
把CString中的文本写入到UTF16BE中
- CStdioFileCodePage file1;
- if (!file1.OpenWithoutType(CppFile, CFile::modeRead))
- {
- file1.Close();
- return false;
- }
- CStdioFile tempFile;
- CString tempFileName = CppFile + L".tmp";
-
- if (!tempFile.Open(tempFileName, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary))
- {
- tempFile.Close();
- return false;
- }
- CString line;
- //写入UTF - 16BE的BOM
- unsigned char bom[2] = { 0xFE, 0xFF };
- tempFile.Write(bom, 2);
- while (file1.ReadStringWithOutType(line))
- {
-
- if (line.Compare(msg3) != 0)
- {
- /* tempFile.WriteString(line + L"\n");*/
- // 将CString对象转换为宽字符格式,并按照大端序写入文件
- CStringW wideStr = CStringW(line + L"\n"); // 将CString换为宽字符格式
- int len = wideStr.GetLength();
- for (int i = 0; i < len; i++)
- {
- unsigned short ch = _byteswap_ushort(wideStr[i]); // 将字节序颠倒
- tempFile.Write(&ch, sizeof(unsigned short)); // 按照大端序写入文件
- }
- }
- else
- {
- break;
- }
- }
-
- tempFile.Close();
- file1.Close();
-
- CFile::Remove(CppFile);
- CFile::Rename(tempFileName, CppFile);
-
- CStdioFile file;
- if (!file.Open(CppFile, CFile::modeWrite | CFile::typeBinary))
- {
- file.Close();
- return false;
- }
-
-
- file.SeekToEnd();
- CStringW wideStr = CStringW(msg); // 将CString换为宽字符格式
- int len = wideStr.GetLength();
- for (int i = 0; i < len; i++)
- {
- unsigned short ch = _byteswap_ushort(wideStr[i]); // 将字节序颠倒
- file.Write(&ch, sizeof(unsigned short)); // 按照大端序写入文件
- }
-
-
- file.Close();
- return true;
注意:写入大端序中的字符顺序需要颠倒
ANSI本地默认编码格式,创建的时候使用CFile::typeText默认选择ANSI编码格式
写入ANSI中需要写入CStringA类型(char *msg也可以)
- CStdioFileCodePage file1;
- if (!file1.OpenWithoutType(CppFile, CFile::modeRead))
- {
- file1.Close();
- return false;
- }
- CStdioFile tempFile;
- CString tempFileName = CppFile + L".tmp";
-
- if (!tempFile.Open(tempFileName, CFile::modeCreate | CFile::modeWrite | CFile::typeText))
- {
- tempFile.Close();
- return false;
- }
-
- CString line;
-
- while (file1.ReadStringWithOutType(line))
- {
-
- if (line.Compare(msg3) != 0)
- {
- tempFile.WriteString(line + L"\n");
- }
- else
- {
- break;
- }
- }
-
- tempFile.Close();
- file1.Close();
-
- CFile::Remove(CppFile);
- CFile::Rename(tempFileName, CppFile);
-
- CStdioFile file;
- if (!file.Open(CppFile, CFile::modeWrite | CFile::typeText| CFile::modeNoTruncate))
- {
- file.Close();
- return false;
- }
- file.SeekToEnd();
- file.Write(msg, strlen(msg));
- file.Close();
- return true;
注意:写入如果发现原先的内容被覆盖说明打开的方式不正确 CFile::modeNoTruncate
与ANSI创建打开文件方法相同,但是区别在于系统默认当前是ANSI,我们需要把CString变量转换成UTF8类型的CStringA变量再写入
- CStdioFileCodePage file1;
- if (!file1.OpenWithoutType(CppFile, CFile::modeRead | CFile::typeText))
- {
- file1.Close();
- return false;
- }
-
- file1.JumpBOM();
-
- CStdioFile tempFile;
- CString tempFileName = CppFile + L".tmp";
-
- if (!tempFile.Open(tempFileName, CFile::modeCreate | CFile::modeWrite | CFile::typeText))
- {
- tempFile.Close();
- return false;
- }
-
- // 设置文件流的locale为UTF-8编码
- //tempFile.WriteString(L"\xEF\xBB\xBF"); // 写入UTF-8的BOM
-
- CString line;
-
- while (file1.ReadStringWithOutType(line))
- {
-
- if (line.Compare(msg3) != 0)
- {
- CStringA lineUTF8 = CW2A(line, CP_UTF8);
- lineUTF8 += L"\n";
- tempFile.Write(lineUTF8,strlen(lineUTF8));
- }
- else
- {
- break;
- }
- }
- // 刷新文件流
- tempFile.Flush();
- tempFile.Close();
- file1.Close();
-
- CFile::Remove(CppFile);
- CFile::Rename(tempFileName, CppFile);
-
- // 打开文件以UTF-8编码形式写入文本数据
- CStdioFile file;
- if (!file.Open(CppFile, CFile::modeWrite | CFile::modeNoTruncate | CFile::typeText))
- {
- file.Close();
- return false;
- }
- file.SeekToEnd();
- // 将msg转换为UTF-8编码的CStringA
- CStringA msgUTF8 = CW2A(msg, CP_UTF8);
- // 使用CStringA来写入UTF-8编码的内容
- file.Write(msgUTF8,strlen(msgUTF8));
- file.Close();
- return true;
相对于UTF8前面有BOM。
- CStdioFileCodePage file1;
- if (!file1.OpenWithoutType(CppFile, CFile::modeRead | CFile::typeText))
- {
- file1.Close();
- return false;
- }
-
- file1.JumpBOM();
-
- CStdioFile tempFile;
- CString tempFileName = CppFile + L".tmp";
-
- if (!tempFile.Open(tempFileName, CFile::modeCreate | CFile::modeWrite | CFile::typeText))
- {
- tempFile.Close();
- return false;
- }
-
-
- tempFile.WriteString(L"\xEF\xBB\xBF"); // 写入UTF-8的BOM
-
- CString line;
-
- while (file1.ReadStringWithOutType(line))
- {
-
- if (line.Compare(msg3) != 0)
- {
- CStringA lineUTF8 = CW2A(line, CP_UTF8);
- lineUTF8 += L"\n";
- tempFile.Write(lineUTF8, strlen(lineUTF8));
- }
- else
- {
- break;
- }
- }
- // 刷新文件流
- tempFile.Flush();
- tempFile.Close();
- file1.Close();
-
- CFile::Remove(CppFile);
- CFile::Rename(tempFileName, CppFile);
-
- // 打开文件以UTF-8编码形式写入文本数据
- CStdioFile file;
- if (!file.Open(CppFile, CFile::modeWrite | CFile::modeNoTruncate | CFile::typeText))
- {
- file.Close();
- return false;
- }
- file.SeekToEnd();
- // 将msg转换为UTF-8编码的CStringA
- CStringA msgUTF8 = CW2A(msg, CP_UTF8);
- // 使用CStringA来写入UTF-8编码的内容
- file.Write(msgUTF8, strlen(msgUTF8));
- file.Close();
- return true;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。