我在Linux系统上,将键盘设置设置为UK,以便捕获并打印出英镑符号(£)。
这是我的代码:
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
int main ()
{
wint_t wc;
fputws (L"Enter text:\n", stdout);
setlocale(LC_ALL, "");
do
{
wc=getwchar();
wprintf(L"wc = %lc %d 0x%x\n", wc, wc, wc);
} while (wc != -1);
return 0;
}
我还想将英镑符号(£)存储为字符串的一部分。我发现存储宽字符时std :: string不能指示准确的大小...在这种情况下使用wstring更好吗?是否提供更准确的尺寸?
答案 0 :(得分:2)
您可以使用std::put_money
#include <iostream>
#include <sstream>
// Include the std::put_money and other utilities
#include <iomanip>
int main(int argc, char **argv)
{
// Value in cents!
const int basepay = 10000;
std::stringstream ss;
// Sets the local configuration
ss.imbue(std::locale("en_GB.utf8"));
ss << std::showbase << std::put_money(basepay);
std::cout << std::locale("en_GB.utf8").name() << ": " << ss.str() << '\n';
return 0;
}
答案 1 :(得分:1)
在获取输入之前先设置语言环境。
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
int main () {
setlocale(LC_ALL, "en_GB.UTF-8");
wchar_t wc;
fputws (L"Enter text:\n", stdout);
do {
wc = getwchar();
wprintf(L"wc = %lc %d 0x%x\n", wc, wc, wc);
} while (wc != -1);
return 0;
}