ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el18jgb

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Highscore.cpp Source File

Highscore.cpp

00001 #include "Highscore.h"
00002 
00003 
00004 Highscore::Highscore()
00005 {
00006 
00007 }
00008 
00009 Highscore::~Highscore()
00010 {
00011 
00012 }
00013 
00014 int Highscore::geths(SDFileSystem &sd)
00015 {
00016     FILE *fp; //file pointer
00017     
00018     fp = fopen("/sd/highscore.txt", "r");
00019     
00020     if (fp == NULL) {  //can't open the file
00021         //printf("cant open file!\n");
00022         int zero = 0;
00023         fp = fopen("/sd/highscore.txt", "w"); //creates a file if there wasnt one
00024         if (fp == NULL){ //if still cant open file there isnt an sd card 
00025             //printf("no sd card\n");
00026         }
00027         else {
00028             fprintf(fp, "%d",&zero); // create a 0 if no value in file
00029             fclose(fp); 
00030         }
00031     } else {
00032             
00033         fscanf(fp, "%d",&stored); // read stored score
00034         //printf("Read %d from file.\n",stored);
00035         fclose(fp); 
00036     }
00037     
00038     return stored;
00039 }
00040 
00041 void Highscore::seths(SDFileSystem &sd, int x)
00042 {
00043     FILE *fp; 
00044     
00045     fp = fopen("/sd/highscore.txt", "w");
00046     int newscore = x;
00047 
00048     if (fp == NULL) {  // should create file if it doesn't exist so error would only occure on lack of sd system
00049         printf("Error! Unable to open file! no sd card inserted\n");
00050     } else {  // file opened
00051         fprintf(fp, "%d",newscore); //write to file
00052         //printf("written %d \n", newscore); 
00053         fclose(fp); 
00054     }
00055 }