简体   繁体   English

ifstream fileOpen导致程序“以异常方式终止”

[英]ifstream fileOpen causes program to 'terminate in an unusual way'

At the line: std::ifstream fileOpen(file.c_str()); 在此行上: std::ifstream fileOpen(file.c_str()); in the below function the program crashes and gives me this error: 在下面的函数中,程序崩溃,并给我这个错误:

This application has requested the Runtime to terminate it in an unusual way. 该应用程序已请求运行时以一种异常方式终止它。 Please contact the application's support team for more information. 请与应用程序的支持团队联系以获取更多信息。


Process exited with return value 3 进程退出,返回值3

However, in debug mode the whole program runs but at the return 0 statement for the main function I get 但是,在调试模式下,整个程序运行,但是在返回的主要函数返回0语句中

Program received signal SIGTRAP, Trace/breakpoint trap. 程序接收到信号SIGTRAP,跟踪/断点陷阱。

I am using Orwell Dev/C++ on Windows 7. From what I can gather the first problem is an exception that is thrown but not caught (I have not yet taught myself about exceptions so I have no idea what to do with this, but I can read up) and that it might be corrupting the stack. 我正在Windows 7上使用Orwell Dev / C ++。据我所知,第一个问题是引发但未被捕获的异常(我尚未对异常进行自我介绍,因此我不知道该如何处理,但我可以读取),并且可能破坏了堆栈。 The latter error I can't get much specific information on. 后一个错误我无法获得很多具体信息。 Could anyone please point me in the direction of a solution? 有人可以指出解决方案的方向吗? Oh, also, the function is called three times before it crashes on the fourth. 哦,同样,该函数在第四次崩溃之前被调用了三次。

//Get a line of data from a file
std::string getData( std::string file, int line )
{
    std::string data;
    std::ifstream fileOpen(file.c_str());

    if (fileOpen.is_open()) 
    {
        if( fileOpen.good() ) 
        {
            for( int lineno = 0; getline(fileOpen,data) && lineno < line; lineno ++ )
            {
                if( lineno != line )
                {
                    data = "";
                }
            }
        }

        fileOpen.close();
    }

    return data;
}

//Parse comma delimited string into a vector
void parseData( std::vector<double> &temp, std::string data ) 
{
    std::istringstream ss(data);
    std::string token;

    while(std::getline(ss, token, ',')) 
    {
        temp.push_back(atoi(token.c_str()));
    }
}

These are called by code like this: 这些通过如下代码调用:

std::string instData = getData( levelName+".dat", 2 );

if( instData != "" ) 
{
    parseData( temp, instData );
    instances.resize(temp.size() / 4);
    j = 0;

    for( int i = 0; i < temp.size(); i += 4 ) 
    {
        instances[ j ].type = temp[ i ];
        instances[ j ].xPos = temp[ i + 1 ];
        instances[ j ].yPos = temp[ i + 2 ];
        instances[ j ].zIndex = temp[ i + 3 ];
        j ++;
    }

    temp.clear();
}

This code itself is part of a function that is designed to fill the various vectors with data from a specified file. 此代码本身是功能的一部分,该功能旨在使用来自指定文件的数据填充各种向量。 The rest of the code in there is essentially the same as the above, though. 不过,其中的其余代码与上面的代码基本相同。

I can clearly see a problem over here: 我可以在这里清楚地看到一个问题:

instances[ j ].xPos   = temp[ i + 1 ];
instances[ j ].yPos   = temp[ i + 2 ];
instances[ j ].zIndex = temp[ i + 3 ];

When i == temp.size() - 3 , the last statement will access a memory region 1 past the end of the allocated memory for temp , causing Undefined Behavior . i == temp.size() - 3 ,最后一条语句将在为temp分配的内存末尾访问内存区域1,从而导致未定义行为 After that happens, your program has entered an invalid state. 在这种情况下,您的程序已进入无效状态。

Getting an error at the line at which you open the file may just be one of the effects of Undefined Behavior. 打开文件所在的行出现错误可能只是“未定义行为”的影响之一。 As a test, remove the above three lines and see if any runtime errors occur. 作为测试,删除以上三行,看看是否发生任何运行时错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 该应用程序已请求运行时以一种异常方式终止它QT C ++ - This application has requested the runtime to terminate it in an unusual way QT C++ 此应用程序已请求Runtime以不寻常的方式终止它。 - This application has requested the Runtime to terminate it in an unusual way. “此应用程序已请求运行时以不寻常的方式终止它。” - “This application has requested the Runtime to terminate it in an unusual way.” Unicode导致关闭消息框终止程序 - Unicode causes closing messagebox to terminate program Shell脚本终止程序,但导致不写入输出文件 - Shell script to terminate program but causes output file to not be written c ++服务正在停止,并显示错误“此应用程序已请求运行时以异常方式终止它” - c++ Service is stopping with error “this application has requested the runtime to terminate it an unusual way” 无法在另一台计算机上运行我的exe文件。 “应用程序请求运行时以不寻常的方式终止它”错误 - Cannot run my exe file on another computer. “Application requested the runtime to terminate it in an unusual way” error 我的Qt C ++程序以异常方式终止 - My Qt C++ program terminates in an unusual way 如何调试“此应用程序已请求运行时以不寻常的方式终止它。” 当我什至无法进入代码时? - How to debug “This application has requested the Runtime to terminate it in an unusual way.” when I can't even step in the code? 是否有 ifstream 和 ofstream 的宏或缩短 ifstream/ofstream 的方法? - Is there a macro for ifstream and ofstream or a way to shorten ifstream/ofstream?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM