Arduino SD card reading and writing

Greetings,
i have a question about SD card reading and writing. So i went through the SD read write example on arduino and tried it out and obviously it works as it should. On the example the "testing" text is written to the SD card in a test.txt file that is created in the setup and it is than read by the arduino and displayed on the serial monitor. My question is..would it be possible to write a function to write to the SD card when a "button" is pressed, and when the arduino is rebooted and the SD read is initialized in the setup, could i "store" the data previously saved on the sd into a variable to use in the program? Or to display on a LCD screen?

Thank you very much in advance

The basic answer is yes. What do you want to store?

Thank you for your reply!

Well bassicly my idea is to make a score counter for a game. When the "game stop" sensor triggers, and if the condition (score>high score) is true, than this is the new high score and has to be saved to SD card. And then it has to be read from the SD card again to be compared to the current score and saved again if score > high score etc.. And the high score would always be displayed on the LCD. However i failed to write a code to do such thing. Any help will be deeply appreciated!

Show your failed attempt so we can point out were you went wrong.

I don't want to get you away from the SD card approach, but have you considered the use of EEPROM (that is available in most Arduinos)?

I have given a thought to using EEPROM but the problem is that it allows only about 100.000 writings and thought that might not be ideal.

However this is my program..

#include <SPI.h>
#include <SD.h>

File myFile;
long score = 1234;
long highscore;
bool reading;
const int button=49;

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
pinMode(49, INPUT_PULLUP);

Serial.print("Initializing SD card...");

if (!SD.begin(53)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");

myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");

// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}

void loop() {
waitforbuttonpress();

}

void waitforbuttonpress()
{
reading=digitalRead(49);
if(reading == 0){ //button was pressed
if(score>highscore){
myFile = SD.open("test.txt");
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println(score);
// close the file:
myFile.close();
}
}
}
}

i dont know how to than use the saved high score to be always compared to the current score on than changed, write to LCD etc..

I have given a thought to using EEPROM but the problem is that it allows only about 100.000 writings and thought that might not be ideal.

How often do you want to update that highscore? And there are ways around it usding wear-leveling.

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }

Instead of printing to Serial, store what is read in a character array till you find a '\r'; below provided as guide. Not compiled nor tested.

    char line[25];
    byte index = 0;
    while (myFile.available())
    {
      // read char and store in line
      line[index]=myFile.read());
      // check if character is '\r'
      if(line[index] == '\r')
      {
        // add terminatin NUL character
        line[index == '\0';
        // done
        break;
      }
      // next character will go in next element of line
      index++;
    }
    highscore = atol(line);

Please note how I posted code using code tags; please do the same in your next post.

Thank you very much for this will give it a try!