visual-c++ - 如何使用 fwprintf 以 utf-8 格式保存文件

标签 visual-c++ utf-8

friend ,

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

最佳答案

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

关于visual-c++ - 如何使用 fwprintf 以 utf-8 格式保存文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23472495/

相关文章:

c++ - WinAPI 和 UTF-8 支持

php - 如何摆脱 json_encode() : Invalid UTF-8 sequence in php?

mysql - 如何将 MySQLworkbench 连接编码设置为 UTF8?

sql - 字符不在轨道 UTF8 中

c++ - Win32 C++ 重绘窗口

c++ - ISO C++ 标准 - 关于检查依赖库的规则。为什么?

c++ - 返回默认构造值有什么问题吗?

c++ - 为什么我在 Microsoft Visual C++ 2010 Express 中收到此消息

源代码中的条件编译

google-app-engine - UTF-8 字符串被 GAE 上的 ReSTLet 扰乱