0

Im following a tutorial series to make a platformer with allegro 5 and when I started making the file manager class it started to break. I managed to pinpoint where it breaks at.

My github is https://github.com/eatmykhack/Platformer/tree/master/Platformer Incase you want to see the rest of my code. Heres the class where I error at.

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
    {

    }
}

It errors at attributes.push_back(tempAttributes); at the bottom. The error I get is

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.

It'll read the file and output "Image" and "Splash/image1.png" but then it breaks.

0 Answers0