設置 | 登錄 | 註冊

作者共發了4篇帖子。

【C++函數整理】C++將寬字符串(wchar_t *和wstring)以UTF8編碼格式寫入txt文本文件的函數fputs_UTF8(不帶頭部BOM信息)

1樓 巨大八爪鱼 2015-12-29 22:44
【函數的三個重載形式】
void fputs_UTF8(const wchar_t *str, FILE *fp);
用於將wchar_t字符數組寫入通過fopen打開的文件中。

void fputs_UTF8(const wchar_t *str, ofstream &file);
用於將wchar_t字符數組寫入通過ofstream打開的文件中。

void fputs_UTF8(wstring &wstr, ofstream &file);
用於將wstring字符串對象寫入通過ofstream打開的文件中。

【使用範例】
// writestr.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <Windows.h>

using namespace std;

void fputs_UTF8(const wchar_t *str, FILE *fp)
{
    int size = WideCharToMultiByte(CP_UTF8, NULL, str, -1, NULL, 0, NULL, NULL);
    char *buffer = (char *)malloc(size * sizeof(char));
    WideCharToMultiByte(CP_UTF8, NULL, str, -1, buffer, size, NULL, NULL);
    fwrite(buffer, size - sizeof(char), 1, fp);
    free(buffer);
}

void fputs_UTF8(const wchar_t *str, ofstream &file)
{
    int size = WideCharToMultiByte(CP_UTF8, NULL, str, -1, NULL, 0, NULL, NULL);
    char *buffer = new char[size];
    WideCharToMultiByte(CP_UTF8, NULL, str, -1, buffer, size, NULL, NULL);
    file.write(buffer, size - 1);
    delete buffer;
}

void fputs_UTF8(wstring &wstr, ofstream &file)
{
    int size = WideCharToMultiByte(CP_UTF8, NULL, wstr.c_str(), -1, NULL, 0, NULL, NULL);
    char *buffer = new char[size];
    WideCharToMultiByte(CP_UTF8, NULL, wstr.c_str(), -1, buffer, size, NULL, NULL);
    file.write(buffer, size - 1);
    delete buffer;
}

int _tmain(int argc, _TCHAR* argv[])
{
    wchar_t wch[] = L"簡體中文ABC";
    FILE *fp;
    fopen_s(&fp, "test/utf8test.txt", "w");
    fputs_UTF8(wch, fp);
    fclose(fp);

    fopen_s(&fp, "test/utf8emptytest.txt", "w");
    fputs_UTF8(L"", fp);
    fclose(fp);

    ofstream file("test/ofstreamutf8test.txt", ios::out);
    fputs_UTF8(wch, file);
    file << endl << "English letters can be directly output.";
    file.close();

    wstring wstr = L"這是一個C++裡面的wstring字符串。";
    file.open("test/ofstreamutf8test2.txt", ios::out);
    fputs_UTF8(wstr, file);
    file << endl << "English letters can be directly output.";
    file.close();
    return 0;
}

【生成的文件】

其中一個文件:


2樓 巨大八爪鱼 2015-12-29 22:47
用Notepad++打開寫入的txt文件,從右下角可以看到確實是UTF-8格式。
並且,utf8test.txt這個文件里的內容是:

大小是15位元組。說明中文占了3個字節,英文字母占了1個字節。
3樓 巨大八爪鱼 2015-12-30 22:58
【讀取UTF8文本文件的方法】
// readstr.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <Windows.h>

using namespace std;

wstring toWideStr(char *str)
{
    int size = MultiByteToWideChar(CP_UTF8, NULL, str, -1, NULL, NULL);
    wchar_t *buffer = new wchar_t[size];
    MultiByteToWideChar(CP_UTF8, NULL, str, -1, buffer, size);
    wstring wstr(buffer);
    delete buffer;
    return wstr;
}

int _tmain(int argc, _TCHAR* argv[])
{
    ifstream file("Monsters.as", ios::in);
    char str[100];
    file.getline(str, 100);
    cout << str << endl;

    setlocale(LC_CTYPE, "chs");
    wstring wstr = toWideStr(str);
    wcout << wstr.c_str() << endl;

    file.close();
    system("pause");
    return 0;
}

輸出:
// 榪欎簺浠g爜鏄€墿緙栬緫鍣ㄧ敓鎴愮殑錛岃涓嶈鎵嬪姩淇敼榪欎簺浠g爜
// 這些代碼是怪物編輯器生成的,請不要手動修改這些代碼
Press any key to continue . . .
4樓 巨大八爪鱼 2016-1-27 13:18
差點忘了,上面的delete都應該改成delete[],因為創建時用的是new []。

內容轉換:

回覆帖子
內容:
用戶名: 您目前是匿名發表。
驗證碼:
看不清?換一張
©2010-2025 Purasbar Ver3.0 [手機版] [桌面版]
除非另有聲明,本站採用知識共享署名-相同方式共享 3.0 Unported許可協議進行許可。