Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 超过15个字符的字符串变为ε■ε■ε■ε■ε■ε■ε■ε■ε_C++_Serialization_Stdstring - Fatal编程技术网

C++ 超过15个字符的字符串变为ε■ε■ε■ε■ε■ε■ε■ε■ε

C++ 超过15个字符的字符串变为ε■ε■ε■ε■ε■ε■ε■ε■ε,c++,serialization,stdstring,C++,Serialization,Stdstring,我最近一直在为基于组件的游戏引擎实现一个工厂。我通过从文件中读入对象所需的组件和初始化对象的内容来反序列化对象。它可以工作,除非我尝试读取长度超过15个字符的属性。在15个字符时,它可以完美地读入,再长一点,我就得到ε■ε■ε■ε■ε■ε■ε■ε■ε作为输出 我使用std::string来存储这些文本行 例如: JUNK组件2测试12345678901234562测试123456789012345 这样一来,test的值就变成了垃圾,而test2则完好无损 知道会发生什么吗 char line[

我最近一直在为基于组件的游戏引擎实现一个工厂。我通过从文件中读入对象所需的组件和初始化对象的内容来反序列化对象。它可以工作,除非我尝试读取长度超过15个字符的属性。在15个字符时,它可以完美地读入,再长一点,我就得到ε■ε■ε■ε■ε■ε■ε■ε■ε作为输出

我使用std::string来存储这些文本行

例如: JUNK组件2测试12345678901234562测试123456789012345 这样一来,test的值就变成了垃圾,而test2则完好无损

知道会发生什么吗

char line[1024];

while (file.getline(line, 1024))
{
    std::vector<std::string> words;
    std::string word;
    int j = 0;
    for (unsigned i = 0; line[i] != '\0' && i < 1024; ++i)
    {

        if (line[i] == ' ' && j > 0 && line[i - 1] != '\\')
        {
            words.push_back(word);
            j = 0;
            word = "";
        }
        else
        {
            ++j;
            word += line[i];
        }
    }
    words.push_back(word);

    // std::cout << (*Parts)["JunkComponent"]->GetName() << std::endl;

    Component* c = (*Parts)[words[0]]->clone(words);
    object->AddComponent(words[0], c);
    for (std::list<Member*>::iterator it = members.begin(); it != members.end(); ++it)
    {
        for (unsigned i = 0; i < words.size(); ++i)
        {
            if ((*it)->GetName() == words[i])
            {
                if (words[i + 1][0] == '\"')
                {
                    std::vector<char> chars;
                    chars.push_back('\"');
                    chars.push_back('\\');
                    for (unsigned int n = 0; n < chars.size(); ++n)\
                    {
                        words[i + 1].erase(std::remove(words[i + 1].begin(), words[i + 1].end(), chars[n]), words[i + 1].end());
                    }
                    Container((*it)->GetMeta(), GET_MEMBER(data.GetData(), (*it)->GetOffset()), (*it)->GetName()).SetValue<std::string>(words[i + 1]);
                }
                else
                {
                    Container((*it)->GetMeta(), GET_MEMBER(data.GetData(), (*it)->GetOffset()), (*it)->GetName()).SetValue<int>(std::stoi(words[i + i]));
                }
                ++i;
                break;
            }
        }
    }
}
SetValue函数:数据为空*


我刚看过你的代码,我就试试看。GET_会员真的很讨厌,我想这就是你的问题所在。它似乎依赖于std::string可以转换为char*,但事实并非如此。为什么您的代码使用小于15的字符串?这很可能是因为大多数流行实现中的std::string实际上包含字符串的一个特例,它保留长度为16 last element\0的内部缓冲区,以避免动态内存分配。当字符串大于15时,此缓冲区将被取消初始化,因为它未被使用。访问字符串的正确方法是使用运算符[]。

请发布代码。它有助于这个过程。好的,这是相当多的,但我会尝试包含所有必要的内容,尝试只包含代码的相关部分。即,与您遇到的问题直接相关的代码。你也可以加入一些你怀疑可能也会导致问题的东西,希望这些代码足够让你了解正在发生的事情。如果需要,我可以编辑和扩展GET_MEMBER宏行null terminated?在同学的帮助下,我找到了这个bug。问题在于SetValue,它是通过存储数据和破坏一切。相反,我这样做了:data=newtdata;
#define GET_MEMBER(P, OFFSET) ((void *)(((char *)(P)) + (OFFSET)))
template <typename T>
void SetValue(T data_)
{
    memcpy(data, &data_, sizeof(T));
}