File handling using FileIO

File handling using FileIO

Oct 25, 2018. | By: Evan Diamond
1 min

Within my text-based adventure game I stored the data of my locations an items in text files. In order to then read in the text files into my game I wrote the implementation below. In this implementation the text files are read first into a buffer which is then streamed into a string stream. Each line of the string stream is then used as one of the arguments passed into the constructor of my Locations class. These lines are for the names of the location.

Code


void GameScene::createLocationData()
{
    Directions directions[MAP_SIZE][4] = {Directions::NONE};
    createLocationDirectionData(directions);//Creates a vector of location directions 

    std::stringstream locations_ss;
    std::string locations_line;
    getFile("/data/Files/Location Data/Locations.txt", locations_ss);
    int loc_count = 0;
    while(getline(locations_ss, locations_line))
    {
        if(locations_line.back() == '\n' || locations_line.back() == '\r')
        {
            locations_line.resize(locations_line.size() - 1);
        }

        setNewLine(locations_line);

        Location* new_loc = new Location(locations_line, directions[loc_count], loc_count
                , loc_count != 32 && loc_count != 38 && loc_count != 43);
        locations[loc_count] = new_loc;
        ++loc_count;
}

About

Gamers developing new games.

Social Links

Our Bunker

University of the West of England,
Frenchay Campus, Bristol, BS16 1QY,
United Kingdom.