2014-05-05 18 views
1

朋友,如何使用fwprintf以UTF-8格式

我在C++初學者,我用VC++ 6.0編寫的UTF-8編碼文件保存文件。

我正在使用fwprintf函數。但該文件正在採用ANSI編碼。任何人都可以告訴我如何使用fwprintf()將文件保存在utf-8中。

這裏是我的代碼,

#include "stdafx.h" 
#include "wchar.h" 
#include "windows.h" 
int main(int argc, char* argv[]) 
{ 
    FILE *file; 
    file = fopen ("abc.txt","w"); 
    fwprintf(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; 
} 
+0

VC 6太舊。這是古老的。如果你可以使用一些現代版本(VS2005,2008,2010,2013),那麼一定要使用它。 – Dialecticus

+0

親愛的朋友們, 謝謝你寶貴的努力,幫助我解決這個問題。 仍然我無法解決它,因爲我使用C風格的程序來解決它,我在vc6.0。 以及我已經瞭解了函數_wfopen(),但是這似乎也不是以utf-8模式創建文件。 至少有一次,如果你能確保我在vc6中沒有api來做到這一點。我將嘗試在vC++ 6.0中重寫我的整個代碼。 等待您的回覆謝謝。 – user3587879

+0

來自Remy Lebeau的第一個代碼示例使用'C',所以它就是你所追求的。 Remy的回答非常完整,沒有任何回覆可以給它增加任何價值。您應該接受該答案,並使用該代碼。 – Dialecticus

回答

0

如果你的編譯器允許它,你可以在fopen通話與"w,ccs=UTF-8"取代"w"

+0

這不適用於VC6; fopen支持UNICODE的流編碼,UTF-8和UTF16-LE直到2005年纔出現。 –

+0

這差不多已經10年了。爲什麼新鑄造的程序員無論如何都會使用舊的編譯器? – Dialecticus

+0

我自己都好奇。 [您可以從MSDN免費獲得最新的VC工具集](http://www.visualstudio.com/en-us/downloads/download-visual-studio-vs.aspx),具有更多的操作系統兼容性和符合標準。我建議Visual Studio Express 2013 for Windows Desktop包編寫控制檯程序。 VC6可以追溯到1998年,對於Windows 2000/XP之後的任何內容都沒什麼用處。 –

2

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; 
} 
相關問題