Ustring错误(打印期间)
Ustring error (during printing)
我想解析UTF-8文件到ustring,我在str中读取了这个文件。出现错误:在引发"Glib::ConvertError"的实例后调用了terminate。我该怎么办?
char* cs = (char*) malloc(sizeof(char) * str.length());
strcpy(cs, str.c_str());
ustring res;
while (strlen(cs) > 0) {
gunichar ch = g_utf8_get_char(cs);
res.push_back(ch);
cs = g_utf8_next_char(cs);
}
wofstream wout("output");
cout << res << endl;
这看起来非常错误:
char* cs = (char*) malloc(sizeof(str.c_str()));
因为sizeof(str.c_str())
必然会给你一些小数字,比如4或8(作为str.c_str()
的结果,以你机器上指针的大小为准)
当然,这并不重要,因为下一行,你正在泄露你刚刚错误分配的内存:
cs = const_cast<char*> (str.c_str());
我还远远不相信您需要const_cast<char *>
(这样做肯定是错误的,因为修改string
中的字符串是未定义的行为)。
相关文章:
- 为什么没有打印错误消息
- 插入排序打印错误的输出
- 当我输入一个字母时,cin 输入(输入是一个 int),而不是打印错误一次,它打印正确一次,然后在循环的其余部分打印 i
- 使用使用运算符<<并打印错误值 c++ 的类进行编程
- cLion & cMake 不调试/打印错误
- 如何使用适用于 Lua 的 C API 在控制台中打印错误
- 为什么我的代码打印错误的密文
- 声明数组和打印[错误?]
- 为什么在这种情况下打印错误
- q调试打印错误的数字
- 阿姆斯特朗数字打印错误
- C++ 链表打印错误
- 长双精度在 MinGW 上打印错误
- C++ <ctime> 打印错误的日期
- 用"fprintf"打印错误后调用"getchar()"有什么意义?
- C++类型包装器 printf 打印错误的浮点值
- 如何让犰狳函数在反转奇异矩阵时不打印错误
- 缩短的数组打印错误的结果
- 打印错误的链表c++
- cout 中的打印错误