m_strName,m_strColl,m_strNO,m_strBan,m_fScore1,m_fScore2,m_fScore3,m_fScore4,m_fScore5,m_fScore6写入文件变乱码怎么修改
时间: 2023-07-15 14:13:03 浏览: 77
出现乱码的原因很可能是文件的编码格式与程序读取文件时指定的编码格式不一致。可以尝试以下几种方法来解决:
1. 指定文件编码格式为 UTF-8 或者其他程序支持的编码格式。
```c++
FILE* fp = fopen("filename.txt", "w, ccs=UTF-8");
```
2. 在写入文件前将字符串转换为程序支持的编码格式。例如,如果程序默认使用的编码格式是 UTF-8,则可以使用以下函数将字符串转换为 UTF-8 格式。
```c++
#include <locale>
#include <codecvt>
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
std::wstring wstr = L"你好";
std::string utf8str = converter.to_bytes(wstr);
```
3. 使用更加稳定的文件写入方式,例如使用 std::ofstream 代替 FILE*。
```c++
#include <fstream>
std::ofstream ofs("filename.txt", std::ios::out | std::ios::binary);
ofs.write((char*)&m_strName, sizeof(m_strName));
ofs.write((char*)&m_strColl, sizeof(m_strColl));
ofs.write((char*)&m_strNO, sizeof(m_strNO));
ofs.write((char*)&m_strBan, sizeof(m_strBan));
ofs.write((char*)&m_fScore1, sizeof(m_fScore1));
ofs.write((char*)&m_fScore2, sizeof(m_fScore2));
ofs.write((char*)&m_fScore3, sizeof(m_fScore3));
ofs.write((char*)&m_fScore4, sizeof(m_fScore4));
ofs.write((char*)&m_fScore5, sizeof(m_fScore5));
ofs.write((char*)&m_fScore6, sizeof(m_fScore6));
ofs.close();
```