struct previousgame {
byte PreviousGame; // 1 or 2
byte PreviousGameNumberOfPlayers; // 1, 2, 3, or 4
String PreviousPlayer1Name; // <= 12 characters
String PreviousPlayer2Name; // <= 12 characters
String PreviousPlayer3Name; // <= 12 characters
String PreviousPlayer4Name; // <= 12 characters
int PreviousHS1; // <= 9999
int PreviousHS2; // <= 9999
int PreviousHS3; // <= 9999
int PreviousHS4; // <= 9999
}
previousgame; // Creates a record
void writepreviousgametoSD() {
myFile = SD.open("newprevi.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to newprevi.txt...");
// Here is where I'd like to write to SD the record previousgame
// close the file:
myFile.close();
Serial.println("done.");
// if this was written successfully:
// delete previous back up file
// rename old file
// rename new file
}
else {
// if the file didn't open, print an error:
Serial.println("error opening newprevi.txt");
}
return;
}
I can write the record one variable at a time but wondered if there was a better way.
Here is what it looks like writing one at a time:
I can write the record one variable at a time but wondered if there was a better way.
Do you want the data in the file in ASCII format, as that code shows, or binary?
If you want ASCII format, you must do it that way. If you want binary format, you can write the struct to the file with one call to write(). Of course, reading it is trickier.
My question was off topic. I just thought you are PaulS - after all. And I don't know about hair or lack of such.
That last & did help my sketch to compile and I was able to save something to SD. And as you said, reading such a binary file was not something I can easily do.
It may help to create a union in which the struct is overlapped with a byte array.
Then you can read/write the same data either as a byte array or as a struct - great scope for a right royal [expletive]-up but it would mean you would just write and read the bytearray to the SD card (or wherever) while having it accessible as a struct elsewhere.