错误:'System.Runtime.InteropServices.SEHException'

error: 'System.Runtime.InteropServices.SEHException'

本文关键字:InteropServices SEHException Runtime System 错误      更新时间:2023-10-16

>我正在尝试以这种方式打开一个文件,但在标记的行上出现异常:

std::string str = "c:/qsdqsdsq/qsdqsdsq/qsdqsd.txt+++++qsdqsdqsd";
FILE *abc = fopen(str.substr(0, str.find_first_of("+++++")).c_str(), "w+b"); // marked line

例外:

类型的未处理异常 "System.Runtime.InteropServices.SEHException"发生在项目中.exe

其他信息:外部组件引发异常。

调试器指向xstring文件的第 2296 行(仅包含_Xout_of_range("invalid string position");行之后的})。

请问我该如何解决这个问题?

多谢!

如果start不在[0, str.length)范围内或start + length不在[0, str.length]范围内,则会引发此异常。由于start为 0,并且find_first_of返回有效的索引或std::string::npos如果在这种情况下找不到任何字符,则表示std::string::npos是由 find_first_of 返回的。由于您发布的代码无法做到这一点,因此我假设您发布的不是导致异常的实际代码。