结构值在c++中不能正确打印

Structure values are not printing correctly in c++

本文关键字:打印 不能 c++ 结构      更新时间:2023-10-16

我有一个字符串消息,我正在分割字符串并将其存储在一个结构中。

 struct logMessage
 {
   int cefVersion;
   char *deviceVendor;
   char *deviceProduct;
   char *deviceVersion;
   int signatureID;
   char *eventName;
   int severity;
   char *objectIp;
   char *cs2;
   char *suser;
   int logonID;
   char *logonType;
}; 

我已经将字符串拆分并将其存储在我的代码结构中,就像这样。

'split(string str)
{       
            string logmsg=str;
    logMessage lmsg;
    string delimiter = "|";
    size_t pos = 0;
    string token;
    int tokens=1;
    while ((pos = logmsg.find(delimiter)) != string::npos) {
            token = logmsg.substr(0, pos);
            cout <<"n"<< token <<endl;
            logmsg.erase(0, pos + delimiter.length());
        switch(tokens){
                case 1:lmsg.cefVersion=atol((char *)token.c_str());
                   cout<<"t token="<<token.c_str();
                   break;
                case 2:lmsg.deviceVendor=(char *)token.c_str();
                   cout<<"t token="<<token.c_str()<<"tlmsg.deviceVendor="<<lmsg.deviceVendor;
                   cout<<"nmessage stored in the sturcture=deviceVendor:"<<lmsg.deviceVendor;
                   break;
                case 3:lmsg.deviceProduct=(char *)token.c_str();
                   cout<<"nmessage stored in the sturcture=deviceProduct:"<<lmsg.deviceProduct;
                   cout<<"t token="<<token.c_str()<<"tlmsg.deviceProduct="<<lmsg.deviceProduct;break;
                case 4:lmsg.deviceVersion=(char *)token.c_str();
                   cout<<"t token="<<token.c_str();break;
                case 5:lmsg.signatureID=atol((char *)token.c_str());
                   cout<<"t token="<<token.c_str();break;
                case 6:lmsg.eventName=(char *)token.c_str();
                   cout<<"t token="<<token.c_str();break;
                case 7:lmsg.severity=atol((char *)token.c_str());
                   cout<<"t token="<<token.c_str();break;
                }   
        tokens++;
        cout<<"ntokens="<<tokens;
        //#cout<<"nmessage stored in the sturcture=deviceProduct:"<<lmsg.deviceProduct;    
    }   
    //#cout<<"nmessage stored in the sturcture=cefVersion:"<<lmsg.cefVersion;
    //#cout<<"nmessage stored in the sturcture=deviceProduct:"<<lmsg.deviceProduct;

    //#cout<<"nmessage stored in the sturcture=signatureID:"<<lmsg.signatureID;
    //cout<<"nmessage stored in the sturcture=eventName:"<<lmsg.eventName;
    //cout<<"nmessage stored in the sturcture=severity:"<<lmsg.severity;
    logmsg=str;
             std::cout << logmsg << std::endl;
}'

在上面的代码中,注释的#行不能正常工作,它正在打印一些其他的值。除此之外,一切都很好,我不明白为什么会发生。

c_str()没有分配新的存储。文档中说:"返回的指针可能会因进一步调用修改对象的其他成员函数而失效。"

。,每次重新分配token时,您已经存储在结构中的char*将不指向任何东西。

您需要为您的每个结构字段分配一个新的char* string,当您填充它时,并从token.c_str() strcpy到它。

您的问题可能是未初始化的变量。当您使用logMessage lmsg;声明结构体时,您只是分配堆栈的一部分。它包含随机数据,而你没有更新它,所以你得到的是随机数据。

您应该为该结构指定一个使用良好默认值的构造函数。或者使用logMessage lmsg{};

对结构体进行零初始化

看起来你是在稍后初始化这些值,但是因为你使用了一个开关,所以不能保证你会运行这些代码

我同意"OmnipotentEntity",你需要在构造函数中初始化它们。

也可以使用字符串代替char *结构的成员?

另外,我会坚持你重新编译代码,你可能会单独编译。c或。cpp。你修改结构了吗?