- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我编写了一个将 wstring 转换为字符串的函数。如果我删除代码 setlocale(LC_CTYPE, ""),程序就会出错。我引用了 cplusplus阅读文档。
C string containing the name of a C locale. These are system specific, but at least the two following locales must exist:
"C" Minimal "C" locale
"" Environment's default localeIf the value of this parameter is NULL, the function does not make any changes to the current locale, but the name of the current locale is still returned by the function.
我的代码在这里,源代码来自cplusplus.com(我添加了一些汉字):
/* wcstombs example */
#include <stdio.h> /* printf */
#include <stdlib.h> /* wcstombs, wchar_t(C) */
#include <locale.h> /* setlocale */
int main()
{
setlocale(LC_CTYPE, "");
const wchar_t str[] = L"中国、wcstombs example";
char buffer[64];
int ret;
printf ("wchar_t string: %ls \n",str);
ret = wcstombs ( buffer, str, sizeof(buffer) );
if (ret==64)
buffer[63]='\0';
if (ret)
printf ("length:%d,multibyte string: %s \n",ret,buffer);
return 0;
}
如果我删除代码 setlocale(LC_CTYPE, ""),程序不会按预期运行。我的问题是:“如果我在不同的机器上运行,程序会有所不同?正如文档所说,如果区域设置为“”,函数不会对当前区域设置进行任何更改,但仍会返回当前区域设置的名称由函数。”因为不同机器的当前语言环境可能不同?
这里是我的c++版本的wstring转wstring,string转wstring不需要函数setlocale,程序运行良好:
/*
string converts to wstring
*/
std::wstring s2ws(const std::string& src)
{
std::wstring res = L"";
size_t const wcs_len = mbstowcs(NULL, src.c_str(), 0);
std::vector<wchar_t> buffer(wcs_len + 1);
mbstowcs(&buffer[0], src.c_str(), src.size());
res.assign(buffer.begin(), buffer.end() - 1);
return res;
}
/*
wstring converts to string
*/
std::string ws2s(const std::wstring & src)
{
setlocale(LC_CTYPE, "");
std::string res = "";
size_t const mbs_len = wcstombs(NULL, src.c_str(), 0);
std::vector<char> buffer(mbs_len + 1);
wcstombs(&buffer[0], src.c_str(), buffer.size());
res.assign(buffer.begin(), buffer.end() - 1);
return res;
}
最佳答案
如果 setlocale 的第二个参数为 NULL,它除了返回当前区域设置外什么都不做。但你没有那样做。您向它发送一个完全由单个零字节组成的字符串,又名 ""
。我的 setlocale 手册页说
If locale is an empty string, "", each part of the locale that should be modified is set according to the environment variables. The details are implementation-dependent.
因此,这为您所做的是将语言环境设置为用户指定的任何内容或系统默认值。
根本不运行 setlocale 可能会使当前语言环境在您的系统上未初始化或为 NULL,这就是为什么您的程序在没有该设置的情况下失败的原因。
你正在使用的东西的另外两个手册页说
The behavior of mbstowcs() depends on the LC_CTYPE category of the current locale.
The behavior of wcstombs() depends on the LC_CTYPE category of the current locale.
如果您根本没有设置语言环境,大概这些例程是失败的。
我猜你可能不需要在每次调用这些例程时都运行 setlocale 语句,但你需要确保它在运行它们之前至少运行一次。
至于根据当前语言环境发生的不同情况,我相信这就是多字节字符串如何准确地转换为宽字符,反之亦然。我认为由于这种差异,这些例程的手册页使其含糊不清。就个人而言,我更喜欢它设置一些示例,例如,“如果当前语言环境是C
,则多字节字符串是ASCII 字符。”我想至少还有一个被解释为 UTF-8,但我对不同的语言环境了解不多,无法准确地说出是哪一个。可能至少还有一种语言环境,其中多字节字符串恰好是每个字符编码的另外两个字节,但 C 和 C++ 仍会将其视为字节。
编辑:再考虑一下,鉴于您添加到示例代码中的字符,明确声明使用不支持中文字符的语言环境将导致最终 printf 报告长度为 -1 可能是有意义的,这包括默认的 C 语言环境。在这种情况下,标准并没有明确指定缓冲区的内容——至少,我对它的阅读表明缓冲区值可能是所有字符,但不包括转换失败的字符。虽然 C++ 文档和 C 文档都没有说明关于无法转换的字符会发生什么。我没有为官方标准付费,但我有最新免费版本的拷贝。 C++17 遵循 C17。 C17 也避免对此功能的这一方面发表评论。对于 wcsrtombs,它明确声明转换状态未指定。然而,在 wcstombs_s 上,C17 状态
If the conversion stops without converting a null wide character and dst is not a null pointer, then a null character is stored into the array pointed to by dst immediately following any multibyte characters already stored.
在我自己对上述 OP 提供的代码进行的实验中,Fedora 28 上的 wcstombs 实现似乎只是避免对缓冲区进行任何进一步的更改。这似乎向我表明,如果代码的确切行为对这种情况很重要,那么使用 wcstombs_s 可能更有意义。但至少,您只需检查返回的长度是否为 -1,如果是,则报告错误而不是假设转换成功。
关于c++ - setlocale 函数有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54319520/
我想弄清楚 C++ 是如何支持 Unicode 的。 当我想将多语言文本输出到控制台时,我调用 std::setlocale .但是我注意到结果取决于之前对 setlocale 的调用. 考虑以下示例
如果我不使用fputws使用setlocale,则仅输出ASCII字母。似乎setlocale是必需的,并且根据this site,setlocale(LC_CTYPE, "UTF-8")和setlo
不允许在共享CentOS服务器上进行编译。因此,我在我的 Debian 计算机上编译我的程序,将其与 Debian 的系统库(如 libc 等)链接。然后我上传我的程序和 Debian 系统库,我的程
我需要为我的应用程序创建一个 SQLite 数据库。我需要用几种欧洲语言存储文本,因此会有大量重音字符和其他奇怪的标记。我正在扩展 SQLiteOpenHelper。 检查 .db 文件时,我注意到有
This paper说 setlocale() 是线程不安全的。是否有任何线程安全的方法来设置语言环境。 我正在用 C++ 编写代码,但如果有任何不同,C 库中的函数将使用语言环境。 这基本上就是我现
这个问题在这里已经有了答案: Is setlocale thread-safe function? (6 个答案) 关闭 6 年前。 CppRef states 2016-12-13 09:00 U
我编写了一个将 wstring 转换为字符串的函数。如果我删除代码 setlocale(LC_CTYPE, ""),程序就会出错。我引用了 cplusplus阅读文档。 C string contai
以下程序使用 setlocale() 从环境变量中获取区域设置,并打印时间。 locale_test.c: // locale test #include #include #include /
我正在学习 C++,我发现了 C++ 库的这个功能:setlocale ( http://www.cplusplus.com/reference/clocale/setlocale/ ) 但我无法理解
我有一个 Linux 系统,该系统设置为某个语言环境并运行一个 C++ 应用程序。我可以从 C++ 应用程序或操作系统本身执行 std::setlocale(LC_NUMERIC, "en_US.UT
我们目前面临的问题是,当使用 Windows 文件打开/保存对话框时加载的外部组件(不幸的是我们不知道是哪个)一些系统更改了进程的区域设置,可能是通过调用 setlocale(LC_ALL, "").
我需要更改线程中的区域设置以正确解析带有 strtod() 的 double ,为此我使用 setlocale() (C++)。它是线程安全的吗? 更新:另一个问题。当我在 main() 函数中调用
我有一些带有(瑞士)法语字符的字符串,我想大写(PHP 5.3)。 echo strtoupper('société'); 由于 strtoupper() 不适用于重字符,我做了一个 setlocal
根据 PHP,“语言环境信息由每个进程维护”。我的理解是否正确,这与使用 Apache 服务器的每个脚本实例相同? 换句话说,如果我有几个使用不同区域设置的并发 session ,一个用户区域设置的更
问题已解决(见下文) 我已经在我的服务器上生成了区域设置,我已经确认它们存在(我的区域设置 -a 在下面提供),但是当我使用时: setlocale(LC_TIME,'fr_FR');
我想在 php 中对包含德语“umlaute”的数组进行排序。这对于php来说似乎不是一件容易的事。我在网上找到了以下示例: $oldLocale=setlocale(LC_COLLATE, "0")
setlocale() 的默认设置是什么意思? setlocale()默认为“C”(“POSIX”)。但这到底是什么意思呢?它的默认字符集和语言是什么?是“en_US.utf8”吗? 最佳答案 来自
setlocale() 函数没有设置所需的语言(德语)。 目标是输出月份名称。 这是我到目前为止的测试代码:
JSTL 标签用于设置用户本地化环境。 语法 JSP 标签的语法如下: 其中: localcode:代表语言代码,例如,ZH、EN。也可以在后面加上国家或者地区的两位数代码,中间用_连接,如
#include #include #include "mainwindow.hpp" #include "../RegisterOfErrors.hpp" #include extern st
我是一名优秀的程序员,十分优秀!