Question

I have a String message and i am splitting the string and storing it in a structure.

 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;
}; 

I have splitted the string and stored it in the structure my code is like this.

'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;
}'

in the above code the commented # line is not working correctly it is printing some other values. except this everything is working fine i didn't understand why it is happening.

Was it helpful?

Solution

c_str() is not allocating new storage. The docs say "The pointer returned may be invalidated by further calls to other member functions that modify the object."

i.e., each time token is reassigned to, the char* you have already stored in your structure are left pointing at nothing.

You need to allocate a new char* string for each of your structure fields when you populate it, and strcpy from token.c_str() to that.

OTHER TIPS

Your issue is probably uninitialized variables. When you declare the struct using logMessage lmsg; you're simply allocating a portion of the stack. It contains random data, and you're not updating it, so you're getting random data back.

You should either specify a constructor that uses good default values for the struct. Or zero-initialize the struct using logMessage lmsg{};

It looks like you're initializing the values later, but because you're using a switch there's no guarantee that you'll run over that code.

I agree with "OmnipotentEntity", you need to initialize them in constructor.

Also can you use string instead of char * for the members of structure?

Additionaly i would insist you to rebuild the code, you might be compiling .c or .cpp indivisually. Did you modify the structure?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top