簡體   English   中英

使用 utf-8 文本文件 C++ 的問題

[英]Problem with working with utf-8 text file c++

最近我在嘗試從 utf-8(?) 格式的文件中獲取行時遇到了一個問題。 我還需要將該字符串復制到剪貼板並能夠將其粘貼到 .txt 文件中。

#include <iostream>
#include <windows.h>
#include <cstdio>
#include <conio.h>
#include <time.h>
#include <string>
#include <fstream>
#include <wchar.h>
using namespace std;
wstring lastLine;



void mesparse()
{
    wifstream client("Client.txt");
    if(client.is_open())
    {
        client.seekg(-7,ios_base::end);
        int kloop=0;
        while (kloop<1)
        {
            wchar_t ch;
            client.get(ch);


            if(ch == '\n') {                    
                kloop=1;                                
            }
            else {                                  
                client.seekg(-4,ios_base::cur);         
            }}            
        getline(client,lastLine);  


        client.close();
    }
    else
    {
        cout<<"Unable to open client.txt file.";
    }
}
void toClipboard(std::wstring s){
    const wchar_t* text = s.c_str();
int len = wcslen(text);

HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, (len + 1) * sizeof(wchar_t));
wchar_t* buffer = (wchar_t*)GlobalLock(hMem);
wcscpy_s(buffer, len + 1, text);
GlobalUnlock(hMem);

OpenClipboard(NULL);
EmptyClipboard();
SetClipboardData(CF_UNICODETEXT, hMem);
CloseClipboard();
}

int main()
{

mesparse();
toClipboard(lastLine);
wcout<<lastLine<<endl;

}

我試圖復制的內容:

йцукaеёśнгшщㅂхфывапрㅊджэячсмитъбюㅗ
йцукaеёśнгшщㅂхфывапрㅊджэя
йцукaеёśнгшщㅂхфывапрㅊ
йцукaеёśнгшщㅂхфыва

CF_UNICODETEXT != UTF-8。

第一個是寬字符,第二個是 8 位。

您首先必須使用MultiByteToWideChar()將其轉換。

因此,將所有文本讀到std::string ,而不是std::wstring 然后使用MultiByteToWideChar()獲取std::wstring然后將其復制到剪貼板。

此外,在 UTF-8 文本中進行字符搜索通常是一個壞主意(變量編碼)。

這是@Michael Chourdakis 指出的解決方案示例代碼。

string mesparse()
{
    string lastLine = "";

    ifstream client("Client.txt");
    if (client.is_open())
    {
        client.seekg(-7, ios_base::end);
        int kloop = 0;
        while (kloop < 1)
        {
            char ch;
            client.get(ch);


            if (ch == '\n') {
                kloop = 1;
            }
            else {
                client.seekg(-4, ios_base::cur);
            }
        }
        getline(client, lastLine);


        client.close();
    }
    else
    {
        cout << "Unable to open client.txt file.";
    }

    return lastLine;
}

void toClipboard(std::string s) {

    int len;
    // Retrieve the length
    len = MultiByteToWideChar(CP_UTF8, MB_PRECOMPOSED, s.c_str(), -1, NULL, 0);

    HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, (len) * sizeof(wchar_t));
    wchar_t* buffer = (wchar_t*)GlobalLock(hMem);

    // Convert to wide char string
    len = MultiByteToWideChar(CP_UTF8, MB_PRECOMPOSED, s.c_str(), -1, buffer, len);
    GlobalUnlock(hMem);

    wcout << buffer << endl;

    OpenClipboard(NULL);
    EmptyClipboard();
    SetClipboardData(CF_UNICODETEXT, hMem);
    CloseClipboard();
}

int main()
{
    string copiedStr = mesparse();
    if (copiedStr.length() == 0)
        return 0;

    toClipboard(copiedStr);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM