C++ 使用 UTF8 BOM 替换 UTF8 文件
void Utf8ToUtf8Bom(const wchar_t* filename) { std::ifstream infile; std::string strline; std::string strresult; // BOM HEADER char c1 = (char)0xEF; char c2 = (char)0xBB; char c3 = (char)0xBF; infile.open(filename); if (infile) { getline(infile, strline); strresult += strline + "\n"; if (strresult[0] == c1 && strresult[1] == c2 && strresult[2] == c3) { return; } while (!infile.eof()) { getline(infile, strline); strresult += strline + "\n"; } // delete "\n" strresult.pop_back(); } infile.close(); // delete old debug.log ::_wremove(filename); std::ofstream outfile(filename); outfile << c1 << c2 << c3; outfile << strresult; outfile.close(); }