【问题标题】:R6010 abort() has been calledR6010 abort() 已被调用
【发布时间】:2015-01-25 12:54:20
【问题描述】:

我从这里读到了 substr

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

这是我的代码:

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

我收到错误:已调用 R6010 abort()

注意:aaa.txt 包含空格/字符/html 标签

有什么想法吗?

【问题讨论】:

  • 如果您提供的子字符串 not 存在,您是否阅读过std::string.find 返回的内容?您期望它不会失败,这可能没有用。
  • 是否每一行都包含文本"test"?如果不是,您认为find 会返回什么,是什么让您认为该值将是substr 的有效输入?
  • 嗯,我明白了,aaa.txt 中有 1 个单词“test”,并且没有重复。那么如何找到它!!!
  • @TharwatHarakeh:你可以通过检查.find()`的返回值找到它。如果且仅当找到该字符串,则返回值将是找到它的位置。
  • 感谢您的提示! <:>

标签: c++ visual-studio-2010 visual-c++ substr


【解决方案1】:

由于我不知道文本文件的内容,您能否尝试进行以下更改,如果仍然显示错误,请告诉我:

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

我正在使用if (startpos != std::string::npos) 条件来检查查找成功时要执行的操作,这在您的代码中缺失。添加此案例将解决您的错误。

继续编码:)

【讨论】:

    【解决方案2】:

    虽然 Code Frenzy 的答案是正确的,但您也可以使用异常来帮助捕获此类错误:

    #include <fstream>
    #include <iostream>
    #include <sstream>
    
    using namespace std;
    
    int main()
    {
        std::ifstream in ("aaa.txt");
        std::ofstream out ("bbb.txt");
        std::string s ;
    
        try
        {
            while ( getline (in,s) )
            {
    
                std::size_t startpos = s.find("test");
    
                std::string str = s.substr (startpos);
    
                out << str << endl;
    
            }
            in.close();
            out.close();
        }
        catch(std::exception e)
        {
            // (1) it will catch the error show show
            cerr << e.what() << endl;
        }
        catch(std::out_of_range e)
        {
            // (2) this will also catch the same error if (1) was not there but could
            // get you more details if you wanted since its more specific but i have
            // not digged into it further
            cerr << e.what() << endl;
        }
        catch(...)
        {
            // (3) just for sanity check if first two didn't catch it
            cerr << "something went wrong";
        }
    }
    

    异常捕获此错误并打印消息:

    字符串位置无效

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多