Visual c++, std和getline(ifstream, string), EOF在std栈内抛出和中断,这正

Visual C++, std and getline(ifstream, string), EOF throws and breaks inside the std stack, is this normal?

本文关键字:std 中断 这正 string getline c++ ifstream Visual EOF      更新时间:2023-10-16

我记得它过去处理字符串和确保在EOF之前添加换行符时所遇到的所有麻烦。

现在我的错误引用互操作,我想知道这是正常的行为仍然或只是一个互操作的"增强",这是我的错误:

A first chance exception of type 'System.Runtime.InteropServices.SEHException' occurred in blah.exe
An unhandled exception of type 'System.Runtime.InteropServices.SEHException' occurred in blah.exe
Additional information: External component has thrown an exception.

不是很有帮助。如果我必须以新行结束,这很酷,但是std库是否已经优化到不能处理奇怪的EOF?

我认为它至少应该抛出一个异常来展开堆栈以找到一个处理程序或终止,但是堆栈仍然在std::函数的深处。这只是一个功能的调试标志在VS?

编辑

    ifstream is("MyfileorSummat.txt");
    string line;
    while (getline(is, line)) { //But when is reads last line @#&#@, it breaks, in the middle of a chain of std calls.

我不知道SEHException是如何相关的,但是您的输入格式是:

std::istream& in;
std::string line;
while (std::getline(in, line)) {
    //stuff
}

这是我所见过的最安全的逐行阅读方法。

I think it should at least throw an exception that unwinds the stack to find a handler or terminate
§15.3(自2011年2月c++草案)

如果没有找到匹配的处理程序,则调用std::terminate()函数;在调用std::terminate()之前是否展开堆栈是由实现定义的(15.5.1)。

这意味着调试器崩溃整个程序的判断是正确的(可能没有展开),但是为了让您看到发生了什么,调试器决定先中断。你必须通过堆栈一步,看看为什么Windows决定抛出一个SEHException。根据一些随机的web抓取,当您访问未分配的内存、解引用NULL指针、内存耗尽或其他情况时,标准库可以触发SEH。您还需要尝试查看SEHException对象的成员,看看状态和消息是什么。