结构值在c++中不能正确打印
Structure values are not printing correctly in c++
我有一个字符串消息,我正在分割字符串并将其存储在一个结构中。
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。你修改结构了吗?
相关文章:
- 为什么Qt Creator的应用程序输出不能从spdlog记录器打印
- 为什么 c++ 不能打印向量 B[0]?
- 为什么我不能提供一个字符串参数来打印 ncurses?
- cout 不能一致地打印字符串和变量值,导致输出未对齐
- 任何人都可以向我解释为什么我不能打印出数组值
- 为什么Ubuntu c++中的eclipse不能打印出数据
- 为什么使用预留分配存储后不能迭代和打印矢量的内容?
- 代码无限循环不能只打印一次
- 为什么我可以打印动态分配的对象,但不能打印使用 cout <<在堆栈上创建的对象
- uint8_t不能用 cout 打印
- C++ : 为什么我不能使用 sprintf 打印 const 字符*?
- 为什么这段代码不能正常打印数字?
- 可以使用 cout 打印,但不能使用相同的对象创建新字符串?
- C++结构,函数不能打印最小直径
- 为什么不能按名称打印成员函数的地址?
- C++多项式类,打印成员函数不能很好地使用幂
- 为什么我不能打印出我的字符串数组 c++?
- 为什么我的打印函数会吐垃圾,而我当前的函数却不能正常工作
- 我不能打印我的书的名字,但可以打印其他所有的东西
- 创建了我自己的向量类.可以重载流运算符,但不能按值c++打印