简体   繁体   中英

ifstream fileOpen causes program to 'terminate in an unusual way'

At the line: 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

However, in debug mode the whole program runs but at the return 0 statement for the main function I get

Program received signal SIGTRAP, Trace/breakpoint trap.

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. 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 . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM