gpt4 book ai didi

c++ - Visual C++、std 和 getline(ifstream, string)、EOF 在 std 堆栈内抛出和中断,这正常吗?

转载 作者:行者123 更新时间:2023-11-28 01:02:11 24 4
gpt4 key购买 nike

我记得过去处理字符串和确保在 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
}

这是我遇到过的最安全的逐行阅读方法。

我认为它至少应该抛出一个异常来展开堆栈以找到处理程序或终止
§ 15.3(自 2011 年 2 月 C++ 草案)

If no matching handler is found, the function std::terminate() is called; whether or not the stack is unwound before this call to std::terminate() is implementation-defined (15.5.1).

这意味着调试器是正确的让您的整个程序崩溃(可能没有展开),但决定友好地中断并向您展示首先发生的事情。您必须遍历堆栈才能准确了解 Windows 决定抛出 SEHException 的原因。根据一些随机网络爬行,当您访问未分配的内存、取消引用 NULL 指针、内存不足或其他情况时,标准库可能会触发 SEH。您还需要尝试查看 SEHException 对象的成员,以查看状态和消息是什么。

关于c++ - Visual C++、std 和 getline(ifstream, string)、EOF 在 std 堆栈内抛出和中断,这正常吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8144234/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com