Simple fish eat program

Dependencies:   mbed mbed-rtos N5110 ShiftReg Tone

classes/HighScore.cpp

Committer:
el18a2k
Date:
2021-04-21
Revision:
12:20ac766b3175
Parent:
11:7c1e2a9303d3
Child:
14:f1552b691274

File content as of revision 12:20ac766b3175:

#include "HighScore.h"

LocalFileSystem local("scores"); //define local file system

//display value in file
void HighScore::displayFile(N5110 &lcd){
    FILE* Scores = fopen("/scores/textfile.txt","r"); //opens and reads file
    fgets(readScore1, 8, Scores); //read first data value and stores it in string 'readScore'
    fclose(Scores); //closes Scores    
        
    lcd.clear();
    lcd.drawRect(0,0,84,48,FILL_TRANSPARENT); //draws screen border
    lcd.printString(" Top Score", 12, 1);
    lcd.printString(readScore1, 30, 3);
    lcd.refresh();
}
    
//write/amend file
void HighScore::writeFile(int score){
    score1 = score;
                                //string            int
    stringCon1 = sprintf(writeScore1, "%i", score1); //converts int into string   
    FILE* Scores = fopen("/scores/textfile.txt","w"); //create a pointer called scores which opens a textfile and writes
    fputs(writeScore1, Scores); //writes string writeScore1 to file
    fclose(Scores);
}

//read from file
int HighScore::readFile(){
    FILE* Scores = fopen("/scores/textfile.txt","r"); //opens and reads file
    fgets(readScore1, 8, Scores); //read first data value and stores it in string 'readScore'
    fclose(Scores); //closes Scores 
    
    //converts string back to int
    readScore = atoi(readScore1);
    
    return readScore;
}