How can I print (☞゚ヮ゚)☞ with Ncurses?

冷暖自知 提交于 2019-12-12 15:03:33

问题


I would like to print (☞゚ヮ゚)☞ with the Ncurses library using C++ in Ubuntu.

First of all, you can do this by simply having:

std::cout << "(☞゚ヮ゚)☞" << std::endl;

And it works just fine.

However, when printing using Ncurses, I think that you need to use printw(char[]). In which case, I try something like this:

std::string str = "(☞゚ヮ゚)☞";   // String
initscr();                     // Start curses mode
printw(str.c_str());           // Print
getch();                       // Wait for input
endwin();                      // Exit curses mode

But it outputs:

(�~X~^��~�~C���~)�~X~^

I had thought that maybe it was c_str()'s fault, but when I do it with std::cout it works just fine too.

How can I print that text with Ncurses? Why does it work with std::cout and not with Ncurses' printw(char[])?

I compile using

g++ Main.cpp -lncurses

In a 64-bit machine. Ubuntu (64 bits too) is running in VirtualBox with OSX as host.


Update:

I've been redirected to https://stackoverflow.com/a/9927113/555690. The solution there doesn't seem to fix my problem - instead, this is how it looks now:

(M-b~X~^M-oM->~M-c~CM-.M-oM->~)M-b~X~^


回答1:


I guess I'll post this as the answer. So, Ubuntu does apparently not by default ship with the Unicode supporting version. So you first need to install it with

sudo apt-get install libncursesw5-dev

then you can compile this

#include <iostream>
#include <string>
#include "locale.h"
#include "ncursesw/ncurses.h"
using namespace std;

int main()
{
    setlocale(LC_ALL, "");
    std::string str = "(☞゚ヮ゚)☞";   // String
    initscr();                     // Start curses mode
    printw(str.c_str());           // Print
    getch();                       // Wait for input
    endwin();  
    return 0;
}

and it'll work without a hitch.

Mind the #include "ncursesw/ncurses.h"



来源:https://stackoverflow.com/questions/19373027/how-can-i-print-%ef%be%9f%e3%83%ae%ef%be%9f-with-ncurses

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!