• MFC中不同编码格式内容的写入


    (1)、UTF16LE

    把CString中的内容写到UTF16LE中去,可以使用WriteString或者Write。

    WriteString函数会把UNICODE字符串以UTF16LE编码格式写入,遇到空字符会提前结束

    Write函数则不受空字符的影响,不会提前结束。

    使用WriteString向UTF16LE中写入CString类型的文本。

    1. CStdioFileCodePage file1;
    2. if (!file1.OpenWithoutType(CppFile, CFile::modeRead))
    3. {
    4. file1.Close();
    5. return false;
    6. }
    7. CStdioFile tempFile;
    8. CString tempFileName = CppFile + L".tmp";
    9. if (!tempFile.Open(tempFileName, CFile::modeCreate | CFile::modeWrite | CFile::typeUnicode))
    10. {
    11. tempFile.Close();
    12. return false;
    13. }
    14. CString line;
    15. while (file1.ReadStringWithOutType(line))
    16. {
    17. if (line.Compare(msg3) != 0)
    18. {
    19. tempFile.WriteString(line + L"\n");
    20. }
    21. else
    22. {
    23. break;
    24. }
    25. }
    26. tempFile.Close();
    27. file1.Close();
    28. CFile::Remove(CppFile);
    29. CFile::Rename(tempFileName, CppFile);
    30. CStdioFile file;
    31. if (!file.Open(CppFile, CFile::modeWrite | CFile::typeUnicode))
    32. {
    33. file.Close();
    34. return false;
    35. }
    36. file.SeekToEnd();
    37. file.WriteString(finamsg);
    38. file.Close();
    39. return true;

    WriteString在写入文本内容后可以指针定位在文件末尾追加

    Write再写入后如果没有包含CFile::modeNoTruncate模式则会选择覆盖原本内容

    使用Write函数向UTF16LE中写入CString内容。

    file.Write(finamsg, finamsg.GetLength() * sizeof(wchar_t));

    内容如果写的不完全注意:UTF16是一个字符由俩个字节表示,所以在第二个参数*2.

    (2)、UTF16BE

    UTF16BE和UTF16LE的区别

    1)、UTF16BE是大端序存储,也就是开头有大端BOM,UTF16LE是小端序存储,也就是开头有小端BOM。

    2)、由于MFC中默认不支持自动添加大端序BOM所以在创建UTF16BE的时候要使用CFile::typeBinary模式来手动处理字节顺序,确保文件以大端字节顺序存储Unicode字符.而创建小端序可以直接使用UNICODE默认的方式创建。

    把CString中的文本写入到UTF16BE中

    1. CStdioFileCodePage file1;
    2. if (!file1.OpenWithoutType(CppFile, CFile::modeRead))
    3. {
    4. file1.Close();
    5. return false;
    6. }
    7. CStdioFile tempFile;
    8. CString tempFileName = CppFile + L".tmp";
    9. if (!tempFile.Open(tempFileName, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary))
    10. {
    11. tempFile.Close();
    12. return false;
    13. }
    14. CString line;
    15. //写入UTF - 16BE的BOM
    16. unsigned char bom[2] = { 0xFE, 0xFF };
    17. tempFile.Write(bom, 2);
    18. while (file1.ReadStringWithOutType(line))
    19. {
    20. if (line.Compare(msg3) != 0)
    21. {
    22. /* tempFile.WriteString(line + L"\n");*/
    23. // 将CString对象转换为宽字符格式,并按照大端序写入文件
    24. CStringW wideStr = CStringW(line + L"\n"); // 将CString换为宽字符格式
    25. int len = wideStr.GetLength();
    26. for (int i = 0; i < len; i++)
    27. {
    28. unsigned short ch = _byteswap_ushort(wideStr[i]); // 将字节序颠倒
    29. tempFile.Write(&ch, sizeof(unsigned short)); // 按照大端序写入文件
    30. }
    31. }
    32. else
    33. {
    34. break;
    35. }
    36. }
    37. tempFile.Close();
    38. file1.Close();
    39. CFile::Remove(CppFile);
    40. CFile::Rename(tempFileName, CppFile);
    41. CStdioFile file;
    42. if (!file.Open(CppFile, CFile::modeWrite | CFile::typeBinary))
    43. {
    44. file.Close();
    45. return false;
    46. }
    47. file.SeekToEnd();
    48. CStringW wideStr = CStringW(msg); // 将CString换为宽字符格式
    49. int len = wideStr.GetLength();
    50. for (int i = 0; i < len; i++)
    51. {
    52. unsigned short ch = _byteswap_ushort(wideStr[i]); // 将字节序颠倒
    53. file.Write(&ch, sizeof(unsigned short)); // 按照大端序写入文件
    54. }
    55. file.Close();
    56. return true;

    注意:写入大端序中的字符顺序需要颠倒

    (3)、ANSI

    ANSI本地默认编码格式,创建的时候使用CFile::typeText默认选择ANSI编码格式

    写入ANSI中需要写入CStringA类型(char *msg也可以)

    1. CStdioFileCodePage file1;
    2. if (!file1.OpenWithoutType(CppFile, CFile::modeRead))
    3. {
    4. file1.Close();
    5. return false;
    6. }
    7. CStdioFile tempFile;
    8. CString tempFileName = CppFile + L".tmp";
    9. if (!tempFile.Open(tempFileName, CFile::modeCreate | CFile::modeWrite | CFile::typeText))
    10. {
    11. tempFile.Close();
    12. return false;
    13. }
    14. CString line;
    15. while (file1.ReadStringWithOutType(line))
    16. {
    17. if (line.Compare(msg3) != 0)
    18. {
    19. tempFile.WriteString(line + L"\n");
    20. }
    21. else
    22. {
    23. break;
    24. }
    25. }
    26. tempFile.Close();
    27. file1.Close();
    28. CFile::Remove(CppFile);
    29. CFile::Rename(tempFileName, CppFile);
    30. CStdioFile file;
    31. if (!file.Open(CppFile, CFile::modeWrite | CFile::typeText| CFile::modeNoTruncate))
    32. {
    33. file.Close();
    34. return false;
    35. }
    36. file.SeekToEnd();
    37. file.Write(msg, strlen(msg));
    38. file.Close();
    39. return true;

    注意:写入如果发现原先的内容被覆盖说明打开的方式不正确 CFile::modeNoTruncate

    (4)、UTF8

    与ANSI创建打开文件方法相同,但是区别在于系统默认当前是ANSI,我们需要把CString变量转换成UTF8类型的CStringA变量再写入

    1. CStdioFileCodePage file1;
    2. if (!file1.OpenWithoutType(CppFile, CFile::modeRead | CFile::typeText))
    3. {
    4. file1.Close();
    5. return false;
    6. }
    7. file1.JumpBOM();
    8. CStdioFile tempFile;
    9. CString tempFileName = CppFile + L".tmp";
    10. if (!tempFile.Open(tempFileName, CFile::modeCreate | CFile::modeWrite | CFile::typeText))
    11. {
    12. tempFile.Close();
    13. return false;
    14. }
    15. // 设置文件流的locale为UTF-8编码
    16. //tempFile.WriteString(L"\xEF\xBB\xBF"); // 写入UTF-8的BOM
    17. CString line;
    18. while (file1.ReadStringWithOutType(line))
    19. {
    20. if (line.Compare(msg3) != 0)
    21. {
    22. CStringA lineUTF8 = CW2A(line, CP_UTF8);
    23. lineUTF8 += L"\n";
    24. tempFile.Write(lineUTF8,strlen(lineUTF8));
    25. }
    26. else
    27. {
    28. break;
    29. }
    30. }
    31. // 刷新文件流
    32. tempFile.Flush();
    33. tempFile.Close();
    34. file1.Close();
    35. CFile::Remove(CppFile);
    36. CFile::Rename(tempFileName, CppFile);
    37. // 打开文件以UTF-8编码形式写入文本数据
    38. CStdioFile file;
    39. if (!file.Open(CppFile, CFile::modeWrite | CFile::modeNoTruncate | CFile::typeText))
    40. {
    41. file.Close();
    42. return false;
    43. }
    44. file.SeekToEnd();
    45. // 将msg转换为UTF-8编码的CStringA
    46. CStringA msgUTF8 = CW2A(msg, CP_UTF8);
    47. // 使用CStringA来写入UTF-8编码的内容
    48. file.Write(msgUTF8,strlen(msgUTF8));
    49. file.Close();
    50. return true;

    (5)、UTF8BOM

    相对于UTF8前面有BOM。

    1. CStdioFileCodePage file1;
    2. if (!file1.OpenWithoutType(CppFile, CFile::modeRead | CFile::typeText))
    3. {
    4. file1.Close();
    5. return false;
    6. }
    7. file1.JumpBOM();
    8. CStdioFile tempFile;
    9. CString tempFileName = CppFile + L".tmp";
    10. if (!tempFile.Open(tempFileName, CFile::modeCreate | CFile::modeWrite | CFile::typeText))
    11. {
    12. tempFile.Close();
    13. return false;
    14. }
    15. tempFile.WriteString(L"\xEF\xBB\xBF"); // 写入UTF-8的BOM
    16. CString line;
    17. while (file1.ReadStringWithOutType(line))
    18. {
    19. if (line.Compare(msg3) != 0)
    20. {
    21. CStringA lineUTF8 = CW2A(line, CP_UTF8);
    22. lineUTF8 += L"\n";
    23. tempFile.Write(lineUTF8, strlen(lineUTF8));
    24. }
    25. else
    26. {
    27. break;
    28. }
    29. }
    30. // 刷新文件流
    31. tempFile.Flush();
    32. tempFile.Close();
    33. file1.Close();
    34. CFile::Remove(CppFile);
    35. CFile::Rename(tempFileName, CppFile);
    36. // 打开文件以UTF-8编码形式写入文本数据
    37. CStdioFile file;
    38. if (!file.Open(CppFile, CFile::modeWrite | CFile::modeNoTruncate | CFile::typeText))
    39. {
    40. file.Close();
    41. return false;
    42. }
    43. file.SeekToEnd();
    44. // 将msg转换为UTF-8编码的CStringA
    45. CStringA msgUTF8 = CW2A(msg, CP_UTF8);
    46. // 使用CStringA来写入UTF-8编码的内容
    47. file.Write(msgUTF8, strlen(msgUTF8));
    48. file.Close();
    49. return true;

  • 相关阅读:
    蓝桥杯每日一题2023.10.22
    PanTools v1.0.27 多网盘批量管理、遍历分享、转存、重命名、复制...
    leetcode做题笔记129. 求根节点到叶节点数字之和
    vue+node.js美食分享推荐管理系统 io551
    【RPA技术】UI分析器与可视化树:解决界面元素获取难题
    Linux命令详解(14)useradd命令
    面试面到了一个腾讯30k出来的,有见识到何为精通MySQL调优
    Jlink_V9固件修复教程
    令人头秃的js隐式转换面试题,你能做对吗
    Window11专业版安装Java环境
  • 原文地址:https://blog.csdn.net/qq_59328991/article/details/136169352