fopen()
不支持編碼在VC6,所以你必須在代碼中手動管理的編碼代替,例如:
#include "stdafx.h"
#include "wchar.h"
#include "windows.h"
bool writeUtf8StrToFile(FILE *file, const wchar_t *str)
{
if (!file) return false;
int wlen = lstrlenW(str);
if (wlen == 0) return true;
int utf8len = WideCharToMultiByte(CP_UTF8, 0, str, wlen, NULL, 0, NULL, NULL);
if (utf8len == 0) return false;
char *utf8 = (char*) malloc(utf8len);
if (!utf8) return false;
utf8len = WideCharToMultiByte(CP_UTF8, 0, str, wlen, utf8, utf8len, NULL, NULL);
if (utf8len == 0) return false;
fwrite(utf8, 1, utf8len, file);
free(utf8);
return true;
}
int main(int argc, char* argv[])
{
FILE *file = fopen ("abc.txt", "wb");
if (file)
{
writeUtf8StrToFile(file, L"This is my utf-8 encoded file");
fclose(file);
WinExec("\"C:\\Program Files\\EditPlus 3\\editplus.exe\" abc.txt", SW_SHOWNORMAL);
}
return 0;
}
或者(因爲你沒有說你是用C++而不是C,其上面的代碼):
#include "stdafx.h"
#include "wchar.h"
#include "windows.h"
#include <fstream>
#include <string>
bool writeUtf8StrToFile(std::ofstream &file, const std::wstring &str)
{
{
if (!file.is_open()) return false;
if (str.empty()) return true;
int utf8len = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), str.length(), NULL, 0, NULL, NULL);
if (utf8len == 0) return false;
std::string utf8(utf8len, '\0');
WideCharToMultiByte(CP_UTF8, 0, str.c_str(), str.length(), &utf8[0], utf8len, NULL, NULL);
file << utf8;
return true;
}
int main(int argc, char* argv[])
{
std::ofstream file("abc.txt", std::ios::out | std::ios::binary);
if (file.is_open())
{
writeUtf8StrToFile(file, L"This is my utf-8 encoded file");
file.close();
WinExec("\"C:\\Program Files\\EditPlus 3\\editplus.exe\" abc.txt", SW_SHOWNORMAL);
}
return 0;
}
或者,使用UTF-8庫從this article,例如:
#include "stdafx.h"
#include "wchar.h"
#include "windows.h"
#include <fstream>
#include <string>
#include <iterator>
#include "utf8.h"
int main(int argc, char* argv[])
{
std::ofstream file("abc.txt", std::ios::out | std::ios::binary);
if (file.is_open())
{
std::wstring utf16str = L"This is my utf-8 encoded file";
std::string utf8str;
utf8::utf16to8(utf16str.begin(), utf16str.end(), std::back_inserter(utf8str));
file << utf8str;
file.close();
WinExec("\"C:\\Program Files\\EditPlus 3\\editplus.exe\" abc.txt", SW_SHOWNORMAL);
}
return 0;
}
或者,使用UTF-8流轉換器從this article,例如:
#include "stdafx.h"
#include "wchar.h"
#include "windows.h"
#include <fstream>
#include "stxutif.h"
int main(int argc, char* argv[])
{
std::wofstream fs("abc.txt", std::ios::out);
if (file.is_open())
{
std::locale utf8_locale(std::locale(), new utf8cvt<false>);
file.imbue(utf8_locale);
file << L"This is my utf-8 encoded file";
file.close();
WinExec("\"C:\\Program Files\\EditPlus 3\\editplus.exe\" abc.txt", SW_SHOWNORMAL);
}
return 0;
}
VC 6太舊。這是古老的。如果你可以使用一些現代版本(VS2005,2008,2010,2013),那麼一定要使用它。 – Dialecticus
親愛的朋友們, 謝謝你寶貴的努力,幫助我解決這個問題。 仍然我無法解決它,因爲我使用C風格的程序來解決它,我在vc6.0。 以及我已經瞭解了函數_wfopen(),但是這似乎也不是以utf-8模式創建文件。 至少有一次,如果你能確保我在vc6中沒有api來做到這一點。我將嘗試在vC++ 6.0中重寫我的整個代碼。 等待您的回覆謝謝。 – user3587879
來自Remy Lebeau的第一個代碼示例使用'C',所以它就是你所追求的。 Remy的回答非常完整,沒有任何回覆可以給它增加任何價值。您應該接受該答案,並使用該代碼。 – Dialecticus