Quantcast
Channel: Active questions tagged utf-8 - Stack Overflow
Viewing all articles
Browse latest Browse all 1027

I have a C++ code in which I am encountering some issues with printing strings

$
0
0

I have this code that I am trying to print some English text alongside text from a CSV file written in Bengali, but I am encountering issues with it.

Enter an English word or phrase (or type 'exit' to quit): homeÓª¼Óª¥Óº£Óª┐UTF-8 String: ├á┬ª┬¼├á┬ª┬¥├á┬º┬£├á┬ª┬┐UTF-16 String: 0x2c34ba8ÓªùÓºâÓª╣Óª¥ÓªùÓª«Óª¿ÓªòÓª░Óª¥UTF-8 String: ├á┬ª┬ù├á┬º┬â├á┬ª┬╣├á┬ª┬¥├á┬ª┬ù├á┬ª┬«├á┬ª┬¿├á┬ª┬ò├á┬ª┬░├á┬ª┬¥UTF-16 String: 0x2c81120Óª©ÓºìÓª¼ÓªªÓºçÓªÂÓª»Óª¥ÓªôÓºƒÓª¥UTF-8 String: ├á┬ª┬©├á┬º┬ì├á┬ª┬¼├á┬ª┬ª├á┬º┬ç├á┬ª┬Â├á┬ª┬»├á┬ª┬¥├á┬ª┬ô├á┬º┬ƒ├á┬ª┬¥UTF-16 String: 0x2c81a80Enter an English word or phrase (or type 'exit' to quit):

That types of format. But i want to print those as well as uft-8

also there is how to my CSV file looks like

col_1,col_2,col_3,col_4,col_5,col_6,col_7,col_8,col_9,a,এক,,,,,,,a,ইংরেজীবর্ণমালারআদ্যাক্ষর,,,,,,,a,ইংরেজিবর্ণমালারপ্রথমঅক্ষর,,,,,,,aback,বিস্মিত,,,,,,,abandon,ত্যাগকরা,,,,,,,abandoned,পরিত্যক্ত,,,,,,,abandonment,বর্জন,,,,,,,abashed,হতভম্ব,,,,,,

c++ code

#include <iostream>#include <fstream>#include <sstream>#include <unordered_map>#include <vector>#include <locale>#include <codecvt>  // For codecvt_utf8std::string convertToUTF8(const std::wstring& wideStr) {    std::wstring_convert<std::codecvt_utf8<wchar_t>> utf8Converter;    return utf8Converter.to_bytes(wideStr);}std::u16string convertToUTF16(const std::wstring& wideStr) {    std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> utf8utf16Converter;    std::string utf8Str = utf8utf16Converter.to_bytes(wideStr);    // Convert UTF-8 to UTF-16    std::u16string utf16Str;    for (char c : utf8Str) {        utf16Str.push_back(static_cast<char16_t>(c));    }    return utf16Str;}// Convert a UTF-8 string to a wide stringstd::wstring utf8ToWstring(const std::string& utf8str) {    std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;    return converter.from_bytes(utf8str);}// Convert a wide string to a UTF-8 stringstd::string wstringToUtf8(const std::wstring& wstr) {    std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;    return converter.to_bytes(wstr);}std::unordered_map<std::wstring, std::vector<std::wstring>> createTranslationDictionary(const std::string& csvFilePath) {    std::unordered_map<std::wstring, std::vector<std::wstring>> wordDict;    std::wifstream file(csvFilePath);    if (!file.is_open()) {        std::cerr << "Error opening CSV file." << std::endl;        return wordDict;    }    std::wstring line;    while (std::getline(file, line)) {        std::wistringstream ss(line);        std::wstring token;        // Assuming CSV format with at least 3 columns        std::vector<std::wstring> row;        while (std::getline(ss, token, L',')) {            row.push_back(token);        }        if (row.size() >= 3) {            std::wstring englishEntry = row[1];            std::wstring bengaliEntry = row[2];            // Handling both single words and phrases            if (englishEntry.find(L',') != std::wstring::npos) {                // If there's a comma in the English entry, treat it as a phrase                std::wistringstream phraseStream(englishEntry);                std::wstring englishWord;                while (std::getline(phraseStream, englishWord, L',')) {                    wordDict[englishWord].push_back(bengaliEntry);                }            } else {                wordDict[englishEntry].push_back(bengaliEntry);            }        }    }    return wordDict;}std::vector<std::wstring> getBengaliEntries(const std::unordered_map<std::wstring, std::vector<std::wstring>>& wordDict, const std::wstring& englishEntry) {    // Function to get Bengali entries for a given English word or phrase    auto iter = wordDict.find(englishEntry);    if (iter != wordDict.end()) {        return iter->second;    } else {        return {englishEntry + L" not found in the dictionary."};    }}int main() {    const std::string csvFilePath = "modified_file.csv";    auto translationDict = createTranslationDictionary(csvFilePath);    while (true) {        // Take user input for English word or phrase        std::wcout << L"Enter an English word or phrase (or type 'exit' to quit): ";        std::wstring userInput;        std::getline(std::wcin, userInput);        if (userInput == L"exit") {            break;        }        // Get and print the corresponding Bengali entries        auto results = getBengaliEntries(translationDict, userInput);        for (const auto& result : results) {            std::wcout << result << std::endl;            // Convert to UTF-8            std::string utf8Str = convertToUTF8(result);            std::cout << "UTF-8 String: " << utf8Str << std::endl;            // Convert to UTF-16            std::u16string utf16Str = convertToUTF16(result);            std::wcout << L"UTF-16 String: " << utf16Str.c_str() << std::endl;        }    }    return 0;}

I try so many thins acally i want to creat a dictionary application but it have this error


Viewing all articles
Browse latest Browse all 1027

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>