尝试从文件读取时出错

时间:2015-03-09 02:33:05

标签: c++ exception file-manager allegro5

我按照教程系列制作了一个带有allegro 5的平台游戏,当我开始制作文件管理器类时,它开始破坏。我设法确定它在哪里突破。

我的github是https://github.com/eatmykhack/Platformer/tree/master/Platformer 如果你想看到我的其余代码。这是我错误的班级。

FileManager.CPP

#include "FileManager.h"
FileManager::FileManager()
{
    identifierFound = false;
}


FileManager::~FileManager()
{
}

void FileManager::LoadContent(const char *filename, std::vector<std::vector<std::string>> &attributes, std::vector<std::vector<std::string>> &contents)
{
    std::ifstream openFile(filename);
    std::string line, newLine;

    if(openFile.is_open())
    {
        while(!openFile.eof())
        {
            std::stringstream str;
            std::getline(openFile, line); 

            if(line.find("Load=") != std::string::npos)
            {
                type = LoadType::Attributes;
                line = line.erase(0, line.find("=") + 1);
                tempAttributes.clear();
            }
            else
            {
                type = LoadType::Contents;
                tempContents.clear();
            }

            str << line;

            while(std::getline(str, newLine, ']'))
            {
                newLine.erase(std::remove(newLine.begin(), newLine.end(), '['), newLine.end());

                std::string erase = " \t\n\r"; 

                newLine.erase(newLine.find_last_not_of(erase) + 1);

                if(type == LoadType::Attributes)
                    tempAttributes.push_back(newLine);
                else
                    tempContents.push_back(newLine);

                std::cout << newLine << std::endl;
            }

            if(type == LoadType::Contents && tempContents.size() > 0)
            {
                attributes.push_back(tempAttributes);
                contents.push_back(tempContents);
            }
        }
    }
    else
    {

    }
}

void FileManager::LoadContent(const char *filename, std::vector<std::vector<std::string>> &attributes, std::vector<std::vector<std::string>> &contents, std::string identifier)
{
    std::ifstream openFile(filename);
    std::string line, newLine;

    if(openFile.is_open())
    {
        while(!openFile.eof())
        {
            std::stringstream str;
            std::getline(openFile, line); 

            if(line.find("EndLoad=") != std::string::npos && line.find(identifier) != std::string::npos)
            {
                identifierFound = false;
                break;
            }
            else if(line.find("Load=") != std::string::npos && line.find(identifier) != std::string::npos)
            {
                identifierFound = true;
                continue;
            }

            if(identifierFound)
            {

                if(line.find("Load=") != std::string::npos)
                {
                    type = LoadType::Attributes;
                    line = line.erase(0, line.find("=") + 1);
                    tempAttributes.clear();
                }
                else
                {
                    type = LoadType::Contents;
                    tempContents.clear();
                }

                str << line;

                while(std::getline(str, newLine, ']'))
                {
                    newLine.erase(std::remove(newLine.begin(), newLine.end(), '['), newLine.end());

                    std::string erase = " \t\n\r"; 

                    newLine.erase(newLine.find_last_not_of(erase) + 1);

                    if(type == LoadType::Attributes)
                        tempAttributes.push_back(newLine);
                    else
                        tempContents.push_back(newLine);

                    std::cout << newLine << std::endl;
                }

                if(type == LoadType::Contents && tempContents.size() > 0)
                {
                    attributes.push_back(tempAttributes);
                    contents.push_back(tempContents);
                }
            }
        }
    }
    else
    {

    }
}

在attributes.push_back(tempAttributes)中出错;在底部。我得到的错误是

First-chance exception at 0x100559da (msvcr100d.dll) in Platformer.exe: 0xC0000005: Access violation reading location 0xcdcdcdc1.
Unhandled exception at 0x100559da (msvcr100d.dll) in Platformer.exe: 0xC0000005: Access violation reading location 0xcdcdcdc1.

它将读取文件并输出“Image”和“Splash / image1.png”但随后它会中断。

0 个答案:

没有答案