Linux中奇怪的iconv_open行为
Weird iconv_open behavior in Linux
在将大型应用程序从Windows移植到Linux时,我需要能够在宽字符和多字节字符之间进行转换。要做到这一点,我有这样的代码:
void IConv(const InType* begin, const InType* end, const char* inCode, OutType* outBegin, OutType*& outEnd, const char* outCode)
{
assert(end >= begin);
assert(outEnd > outBegin);
iconv_t cd = iconv_open(outCode, inCode);
if (cd == reinterpret_cast<iconv_t>(-1))
throw (InvalidLocale ());
/* blah, blah, blah other code we never reach */
}
该代码总是抛出异常。为了调试它,我创建了一个更简单的版本,它使用与失败代码相同的参数。这是我的测试代码
int main( void )
{
const char outCode[] = "";
const char inCode[] = "wchar_t";
//Using wchar_t and "" means that iconv will just use the system locale settings.
iconv_t cd = iconv_open(outCode, inCode);
if (cd == reinterpret_cast<iconv_t>(-1))
{
printf("iconv failed to use outCode %s and inCode %sn",outCode, inCode);
return 1;
}
iconv_close(cd);
return 0;
}
请注意,代码基本相同。但在我的测试代码中,我从未看到过失败,而IConv函数总是失败。系统上的区域设置是通过LANG-env变量设置的,在本例中该变量始终为ISO-8859-1。
所以,问题是,有人知道iconv中的任何特定行为可能会出现在一个大型应用程序中,但不是在一个简单的案例中吗?感谢
问题可能是目标计算机没有安装适当的iconv库和索引。参见/usr/lib[64]/gconv
。共享库通常是glibc
安装的一部分。localedef
等工具可以创建它们。
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- arr[-1]在c++中的奇怪行为
- 继承期间显示未知行为的子类
- 如何在c++中使用引用实现类似python的行为
- G锁定铸造到基础上会释放模拟行为
- 在C++中对T*类型执行std::move的意外行为
- std::当在256字节边界上写入整数时,流的奇怪行为
- 不知道某个东西是否被忽略会引入未定义的行为吗
- 奇怪的构造函数行为
- 重载运算符new[]的行为取决于析构函数
- 不同语言中相同代码的不同行为
- 处理除以零会导致<csignal>意外行为
- 试图理解类对象的行为
- c++11评估顺序(未定义的行为)
- 从结构寻址时,MMAP变量的行为很奇怪
- 我可以做些什么来消除或最小化这种将提供相同功能和行为的代码重复
- 读取文件时运行时的未知行为
- strncpy之后的char数组的错误行为
- 将const char *传递给file.open(),奇怪的行为