C++ Ustring错误(打印期间)

C++ Ustring错误(打印期间),c++,utf-8,glib,wofstream,glibmm,C++,Utf 8,Glib,Wofstream,Glibmm,我想将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);

我想将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(char)*str.length());
strcpy(cs,str.c_str());
ustring res;
而(strlen(cs)>0){
gunichar ch=g_utf8_get_char(cs);
res.推回(ch);
cs=g_utf8_next_char(cs);
}
wofstream wout(“输出”);
cout这看起来非常错误:

char* cs = (char*) malloc(sizeof(str.c_str()));
as
sizeof(str.c_str())
必然会给您一些小数字,如4或8(以计算机上指针的大小为准,作为
str.c_str()
的结果)

当然,这并不重要,因为在下一行中,您正在泄漏刚刚分配不正确的内存:

cs = const_cast<char*> (str.c_str());
cs=const_cast(str.c_str());
我并不认为您需要
常量转换(这样做肯定是错误的,因为在
字符串中修改字符串是未定义的行为)。

根据本页:converterror包含一些关于“什么是错误”的额外信息,这可能有助于确定错误的实际原因。