首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用utf-8格式的fwprintf保存文件

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

Stack Overflow用户
提问于 2014-05-05 12:29:46
回答 2查看 1.5K关注 0票数 3

朋友,

我是c++的初学者,我用vc6.0编写了一个以utf-8编码的文件。

为此,我使用fwprintf函数。但是文件是在ANSI编码中出现的。有人能告诉我如何使用fwprintf()将文件保存在utf-8中吗?

这是我的密码

代码语言:javascript
复制
#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;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-05-05 13:06:55

如果编译器允许,您可以在"w"调用中用"w,ccs=UTF-8"替换fopen

票数 2
EN

Stack Overflow用户

发布于 2014-05-06 01:55:19

fopen()不支持VC6中的编码,因此您必须用代码手动管理编码,例如:

代码语言:javascript
复制
#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 ):

代码语言:javascript
复制
#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库,例如:

代码语言:javascript
复制
#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流转换器,例如:

代码语言:javascript
复制
#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;
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23472495

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档