C++ – R6010 abort() has been called

c++substrvisual-c++visual-studio-2010

I read about substr from here

http://www.cplusplus.com/reference/string/string/substr/

Here is my code :

 int main()
{
std::ifstream in ("c:\\users\\admin\\desktop\\aaa.txt");
std::ofstream out ("c:\\users\\admin\\desktop\\bbb.txt");
std::string s ;
while ( getline (in,s) )
{

    std::size_t startpos = s.find("test");

    std::string str = s.substr (startpos);

    out << str << endl;

}
  in.close();
 out.close();
}

I get error : R6010 abort() has been called

Note : aaa.txt contains spaces/characters/html tags

Any idea ?

Best Solution

Since I dont know the content of the text file, could you try making the following changes and let me know if the error is still being shown:

#include <fstream>
#include <iostream>
#include <sstream>

using namespace std;

int main()
{
    ifstream in("example.txt");
    ofstream out("bbb.txt");
    string s = std::string();
    string str = std::string();
    while (getline(in, s))
    {
        size_t startpos = s.find("test");
        cout << s;

        if (startpos != std::string::npos){
            str = s.substr(startpos);
            out << str << endl;
        }
    }
    in.close();
    out.close();
    getchar();

    return 0;
}

I am using if (startpos != std::string::npos) condition to check what to do when the find succeeds, this is missing in your code. adding this case will resolve your error.

Keep coding :)

Related Question