如何使用utf-8格式的fwprintf保存文件

时间:2014-05-05 12:29:47

标签: visual-c++ 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;
}

2 个答案:

答案 0 :(得分:3)

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;
}

或者,使用this article中的UTF-8库,例如:

#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;
}

或者,使用this article的UTF-8流转换器,例如:

#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;
}

答案 1 :(得分:1)

如果您的编译器允许,您可以在"w"来电中将"w,ccs=UTF-8"替换为fopen