出现乱码的原因是在insertString函数中,当插入的字符串长度超过当前结点的最大容量时,会创建新的结点并将剩余的字符串拷贝到新结点中。但在新结点中,没有将剩余的字符设置为结束字符\0,导致乱码的输出。

为了解决这个问题,可以在创建新结点后,将剩余的字符设置为结束字符\0,即添加一行代码newNode->content[i - MAX_SIZE] = '\0';

以下是修改后的代码片段:

if (str[i] != '\0') { // 存在剩余未插入的字符串
    newNode->content[0] = '\0';
    newNode->next = editor->current->next;
    newNode->prev = editor->current;
    editor->current->next = newNode;
    if (newNode->next != NULL) {
        newNode->next->prev = newNode;
    }
    editor->current = newNode;
    editor->totalNodes++;
    
    i++;
    while (str[i] != '\0' && i < MAX_SIZE) { // 拷贝剩余字符串到新结点
        editor->current->content[i - MAX_SIZE] = str[i];
        i++;
    }
    editor->current->content[i - MAX_SIZE] = '\0';
}

通过添加这行代码,保证了新结点中的字符数组以\0结尾,避免了乱码的输出。

希望这能解决你的问题。如果还有其他疑问,请随时提问。


原文地址: http://www.cveoy.top/t/topic/bcHO 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录