简体   繁体   中英

rfind and << crashing program

I have a problem that has a few programmers pulling their hair. The language is C/C++. Consider the code below...assume request is a valid C++ string.

string outMsg;
string trans_str = ("</TRANSACTION>");
int32_t position;

//Assign outMsg and find "</TRANSACTION>"
outMsg.assign(request);
position = outMsg.rfind(trans_str);

The program crashes on the rfind(trans_str). The program also crashes when find(...) and replace(...) is used. It seems it has trouble with most C++ methods. The program also crashes on lines like the following....

cout << "This is a string " << variable << "and this is the end"; //this is an example

The program usually crashes on the third "<<" and would only print the following output in this example:

"This is a string {variable} " CRASH.

A lot of weird things going on here.

"string" (in angle brackets) is included.

Thank you!

Your code snippets are very incomplete, but when I fill in the blanks with what I think might make sense, it runs fine for me.....

#include <iostream>
#include <string>

int main()
{
    std::string outMsg;
    std::string trans_str = ("</TRANSACTION>");
    int32_t position;

    std::string request = "abcdefg</TRANSACTION>hijklmnop";

    //Assign outMsg and find "</TRANSACTION>"
    outMsg.assign(request);
    position = outMsg.rfind(trans_str);

    std::string variable = outMsg.substr(position, outMsg.size()-position);

    std::cout << "This is a string\n" << variable << "\nand this is the end\n"; //this is an example
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM