I've been writing a program in c++ and decided that using curses would be a better way to do things (to make things cross-platform). It mostly works fine, but there's one problem. In my original code, whenever I wrote std::cout << char(30)
it would output ▲
. I used this in parts of my program. Now, when I use addch(char(30))
, it writes ^[something I don't know because it's overwritten by something else]
.
I've been searching for a long time on how to fix this problem. People say some special characters that curses includes, but none of them are what I want. People say that I should try to use -lncursesw
, but my compiler can't find it. I know that this is a unicode character, but how come I was able to use it before?
I'm on Windows using mingw64 to compile my code. I'm fine with many different ways of fixing it, as long as it's cross-platform. I could try to find some way of making unicode work (but the fact that I can't use -lncursesw
seems to say I can't). I'm okay with including a character table, but I don't know how to do this or how to make curses use it. In the end, I'm just wanting to know: How can I use these special characters that I can normally use with cout
?
You're depending on a particular code page or character set. The portable way to fix this is to use wide characters and the Unicode equivalent. Try this:
#include <iostream>
#include <locale>
int main()
{
constexpr wchar_t uptriangle = L'\u25B2';
std::locale::global(std::locale(""));
std::wcout.imbue(std::locale());
std::wcout << uptriangle << std::endl;
return 0;
}
You would either need to use the wide-character version of ncurses or output u8"\▂"
in a version that supports UTF-8, such as Windows with code page 65001 or Unix with a UTF8 locale.
The wide-character calls supported by ncurses include wadd_wch
and waddwstr
, which generally support UTF-8 encoding. If you're using the "ncurses" library, it's likely that there is an "ncursesw" for the same platform (even mingw, though this page seems to have overlooked building/providing that: the instructions omit the --enable-widec
option for instance).
The addch
function accepts only an 8-bit character value. Your parameter does not fit into an 8-bit value. ncurses (unlike X/Open) allows a sequence of 8-bit values to be passed to addch
and interpreted as a multibyte character (such as UTF-8).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.