std::string append problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rsennat
    New Member
    • Dec 2007
    • 14

    std::string append problem

    Hi,
    I have a strange problem with strings concatenation. I have a xmlobj, which contains request, response, status, notification and control which are all of type std::string.

    When I dump that, it prints every member variable properly. But when I concatenate as below, "response" string alone is not appended. though the data is there.

    Please help me to fix this problem
    Thanks
    rsennat

    Code:
        xmlobj.XmlDump();
        string xmlMessage;
        xmlMessage.reserve(300);
    
        xmlMessage = "<tagmessage>";
    
        xmlMessage += "<tagrequest>";
        xmlMessage += xmlobj.GetRequest();
        xmlMessage += "</tagrequest>";
    
        xmlMessage += "<tagstatus>";
        xmlMessage += xmlobj.GetStatus();
        xmlMessage += "</tagstatus>";
    
        xmlMessage += "<tagnotification>";
        xmlMessage += xmlobj.GetNotifMsg();
        xmlMessage += "</tagnotification>";
    
        xmlMessage += "<tagcontrol>";
        xmlMessage += xmlobj.GetCtrlMsg();
        xmlMessage += "</tagcontrol>";
    
        xmlMessage += "<tagresponse>";
        xmlMessage += xmlobj.GetResponseMsg(); [B]--> not appended here[/B]
        cout << "IN XML ENCODE: " << xmlobj.GetResponseMsg().c_str() << endl;  [B]--> this prints the data properly[/B]
        xmlMessage += "</tagresponse>";
    
        xmlMessage += "</tagmessage>";
    
        printf("XML message is:: %s\n", xmlMessage.c_str());
  • rsennat
    New Member
    • Dec 2007
    • 14

    #2
    It got fixed when I used it as,
    xmlMessage.appe nd(xmlobj.GetRe sponseMsg().c_s tr());
    instead of
    xmlMessage.appe nd(xmlobj.GetRe sponseMsg());

    How there could be a different behaviour when using a "string" or a "const char*" for the string api's.

    thanks
    rsennat

    Originally posted by rsennat
    Hi,
    I have a strange problem with strings concatenation. I have a xmlobj, which contains request, response, status, notification and control which are all of type std::string.

    When I dump that, it prints every member variable properly. But when I concatenate as below, "response" string alone is not appended. though the data is there.

    Please help me to fix this problem
    Thanks
    rsennat

    Code:
        xmlobj.XmlDump();
        string xmlMessage;
        xmlMessage.reserve(300);
    
        xmlMessage = "<tagmessage>";
    
        xmlMessage += "<tagrequest>";
        xmlMessage += xmlobj.GetRequest();
        xmlMessage += "</tagrequest>";
    
        xmlMessage += "<tagstatus>";
        xmlMessage += xmlobj.GetStatus();
        xmlMessage += "</tagstatus>";
    
        xmlMessage += "<tagnotification>";
        xmlMessage += xmlobj.GetNotifMsg();
        xmlMessage += "</tagnotification>";
    
        xmlMessage += "<tagcontrol>";
        xmlMessage += xmlobj.GetCtrlMsg();
        xmlMessage += "</tagcontrol>";
    
        xmlMessage += "<tagresponse>";
        xmlMessage += xmlobj.GetResponseMsg(); [B]--> not appended here[/B]
        cout << "IN XML ENCODE: " << xmlobj.GetResponseMsg().c_str() << endl;  [B]--> this prints the data properly[/B]
        xmlMessage += "</tagresponse>";
    
        xmlMessage += "</tagmessage>";
    
        printf("XML message is:: %s\n", xmlMessage.c_str());

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Originally posted by rsennat
      xmlMessage += xmlobj.GetRespo nseMsg(); --> not appended here
      cout << "IN XML ENCODE: " << xmlobj.GetRespo nseMsg().c_str( ) << endl; --> this prints the data properly
      So are you saying that:
      [code=cpp]
      xmlMessage += xmlobj.GetRespo nseMsg(); --> not appended here
      cout << "IN XML ENCODE: " << xmlobj.GetRespo nseMsg() << endl;
      [/code]

      does not print the data correctly???

      Comment

      Working...