Hey there friends! I need your help in SD card!
I have a text file named party_a.txt in my sdcard. read/write scketch goes fine.
My objective: Need to read the value stored in party_a.txt then increment it by value one and store it back on the same text file.
I tried all sorts of things but no luck!
These are what i tried:
#include <SPI.h>
#include <SD.h>
File myFile;
char a;
void setup()
{
Serial.begin(9600);
while (!Serial) {
;
}
Serial.print("Initializing SD card...");
pinMode(53, OUTPUT);
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
myFile = SD.open("party_a.txt");
if (myFile) {
Serial.print("party_a.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
a=myFile.read();
// Serial.print(a);
}
Serial.println(a);
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening part_a.txt");
}
}
void loop()
{
}
The output:
Initializing SD card...initialization done.
party_a.txt:
And for this code:
myFile = SD.open("party_a.txt");
if (myFile) {
Serial.print("party_a.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
a=myFile.read();
Serial.print(a);
}
// Serial.println(a);
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening part_a.txt");
}
The output is
Initializing SD card...initialization done.
party_a.txt:12
which is fine....
I just want to increment the value stored in a text file... Please help!