Is there any difference between wide characters in GCC and MSVC? In my code, I use wstring and wcout to process and output wide string. I found that compiling my code with GCC (Release mode) can't output them normally, but MSVC (All modes) can.
What can I do to make the executable compiled using GCC (Release mode) work properly?
Code snippet:
bool WCH_FileIO(string _file_name, string _mode, function <void()> _func) {
// Open file.
if (_mode == "r" || _mode == "Wr") {
fin.open(_file_name, ios::in);
if (_mode == "Wr") {
wfin.open(_file_name, ios::in);
if (wfin.is_open()) {
return false;
}
}
if (fin.is_open()) {
_func();
fin.close();
if (_mode == "Wr") {
wfin.close();
}
return true;
} else {
return false;
}
} else if (_mode == "w" || _mode == "Ww") {
fout.open(_file_name, ios::trunc | ios::out);
if (_mode == "Ww") {
wfout.open(_file_name, ios::trunc | ios::out);
if (wfout.is_open()) {
return false;
}
}
if (fout.is_open()) {
_func();
fout.close();
if (_mode == "Ww") {
wfout.close();
}
return true;
} else {
return false;
}
} else if (_mode == "a" || _mode == "Wa") {
fout.open(_file_name, ios::app | ios::out);
if (_mode == "Wa") {
wfout.open(_file_name, ios::trunc | ios::out);
if (wfout.is_open()) {
return false;
}
}
if (fout.is_open()) {
_func();
fout.close();
if (_mode == "Wa") {
wfout.close();
}
return true;
} else {
return false;
}
} else {
return false;
}
}
void WCH_ow() {
// Get a random sentence.
try {
URLDownloadToFile(0, "https://v1.hitokoto.cn/?encode=text", "WCH_STDL.tmp", 0, 0);
WCH_cmd_line = false;
WCH_Sleep(500);
WCH_FileIO("WCH_STDL.tmp", "r", [&]() {
string res;
getline(fin, res);
wcout << StrToWstr(UTF8ToANSI(res)) << endl;
});
DeleteFile("WCH_STDL.tmp");
WCH_cmd_line = true;
} catch (...) {
WCH_Error(WCH_ERRNO_NETWORK_FAILURE);
return;
}
}
void WCH_update() {
// Visit the website to update the program.
try {
cout << "Checking update..." << endl;
WCH_ProcessBarTot = 5;
thread T(WCH_ProcessBar);
T.detach();
URLDownloadToFile(0, "https://class-tools.gq/update/WCH", "WCH_UPD.tmp", 0, 0);
string res;
WCH_FileIO("WCH_UPD.tmp", "r", [&]() {
getline(fin, res);
if (res != WCH_VER) {
WCH_Sleep(5000);
system("start https://github.com/class-tools/Web-Class-Helper/releases/latest/");
WCH_printlog(WCH_LOG_MODE_UPD, {"Updating to version", res});
} else {
WCH_Sleep(5000);
cout << "Already up to date." << endl;
WCH_printlog(WCH_LOG_MODE_UPD, {"Program version is already", res});
}
});
DeleteFile("WCH_UPD.tmp");
} catch (...) {
WCH_Error(WCH_ERRNO_NETWORK_FAILURE);
return;
}
}
GCC:
MSVC:
Please ignore these output order bugs.