SL2048.cpp 886 Bytes
Newer Older
Kyle Malaguit's avatar
Kyle Malaguit committed
1 2 3 4 5 6 7 8 9 10
#include "SL2048.h"

void SL2048::LoadContainers(){
    ifstream fin;
    fin.open("2048Data.txt");
    if(!fin.is_open()){
        cerr << "Could not open file.\n";
        exit(0);
    }
    string blank;
Kyle Malaguit's avatar
Kyle Malaguit committed
11
    string name;
Kyle Malaguit's avatar
Kyle Malaguit committed
12 13 14 15 16 17 18 19 20 21
    int score;
    
    fin >> blank;
    fin >> blank;
    
    //until file is empty load into conatiners
    while(fin >> name){
        //prep variables to store
        fin >> score;
        //put into vector
Kyle Malaguit's avatar
Kyle Malaguit committed
22
        Scores newScore(name, score);
Kyle Malaguit's avatar
Kyle Malaguit committed
23 24 25 26 27 28
        scores.push_back(newScore);
    }
    
    fin.close();
}

Kyle Malaguit's avatar
Kyle Malaguit committed
29 30
void SL2048::updateFile(string fileName){
    
Kyle Malaguit's avatar
Kyle Malaguit committed
31
    ofstream fout;
Kyle Malaguit's avatar
Kyle Malaguit committed
32
    fout.open(fileName, ofstream::out | ofstream::trunc);
Kyle Malaguit's avatar
Kyle Malaguit committed
33
    //delete file contents
Kyle Malaguit's avatar
Kyle Malaguit committed
34
    fout << "NAME" << "\t" << "SCORE" << endl;
Kyle Malaguit's avatar
Kyle Malaguit committed
35
    for(auto scoresFull : scores){
Kyle Malaguit's avatar
Kyle Malaguit committed
36
        fout << scoresFull.getName() << "\t" << scoresFull.getScore() << endl;
Kyle Malaguit's avatar
Kyle Malaguit committed
37 38 39 40
    }
    fout.close();
    
}