Alright,
Yesterday I made a post, which I will be redirecting here now for others who might have similar inquiries (regardless if this thread gets going or not).
In my ignorance and haste I believed that I was able to delete lines from a text file successfully, I was wrong
Here is the code that I believed was working:
#include "application.h"
#include "sd-card-library.h"
File myFile;
// SOFTWARE SPI pin configuration - modify as required
// The default pins are the same as HARDWARE SPI
const uint8_t chipSelect = A2; // Also used for HARDWARE SPI setup
const uint8_t mosiPin = A5;
const uint8_t misoPin = A4;
const uint8_t clockPin = A3;
char check = 0;
int i =0;
String dataString = "";
SYSTEM_MODE(SEMI_AUTOMATIC);
void setup()
{
Serial.begin(115200);
while (!Serial.available()) SPARK_WLAN_Loop();
Serial.print("Initializing SD card...");
// Initialize SOFTWARE SPI
if (!SD.begin(mosiPin, misoPin, clockPin, chipSelect)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
myFile = SD.open("test.txt", FILE_WRITE);
if (myFile) { // if the file opened okay, write to it:
Serial.print("Writing to test.txt...");
for (int i=0; i<1; i++){
myFile.println("testing 1, 2, x.");
myFile.println("testing 1, 2, z.");
myFile.println("testing 1, 2, g.");
myFile.println("testing 1, 2, h.");
myFile.println("testing 1, 2, j.");
myFile.println("testing 1, 2, n.");
myFile.println("testing 1, 2, k.");
myFile.println("testing 1, 2, u.");
myFile.println("beep boop beep, 1, 2, 3.");
myFile.println("testing 1, 2, 3 please.");
}
myFile.close(); // close the file: closing the file is what actually saves the data on the SD
Serial.println("done."); // ^^ you can .close() to save or .flush() to save
} else {
Serial.println("error opening test.txt"); // if the file didn't open, print an error:
}
myFile = SD.open("test.txt"); // re-open the file for reading:
if (myFile) {
Serial.print("Current File postion For reading: "); Serial.println(myFile.position());
Serial.println("test.txt:");
while (myFile.available()) { // read from the file until there's nothing else in it:
Serial.write(myFile.read());
}
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
myFile = SD.open("test.txt", FILE_WRITE); // need a bitwise or ('|') in order to use O_RDWR not the boolean operand ('||')
if(myFile)
{
Serial.print("Current File postion for writing: "); Serial.println(myFile.position());
Serial.println("\nNow delete first five lines . . .");
myFile.seek(0); // go to the start of the file
Serial.print("File postion after seek: "); Serial.println(myFile.position());
while (i<5)
{
dataString = "";
//myFile.seek(0);
while (myFile.available())
{
check = myFile.peek();
dataString += String (check);
Serial.println(dataString);
myFile.write(8);
if (check == '\n') break;
}
i++;
}
Serial.println("Did the file close?"); //Yes it did
myFile.close();
}
else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
while (myFile.available()) { //SEe if we deleted the lines
Serial.write(myFile.read());
}
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
//SD.remove("test.txt"); //remove the file so that on reset, we can see if 5 lines were deleted easily
}
void loop()
{
// nothing happens after setup
}
I did varying lines of text to write in “test.txt”, not just the ones shown above; but the code SEEMED to work at first glance.
If anyone uses this code they will see that on the Serial Monitor (I was using PuTTY) that it will show10 unique lines originally in the text file, then it will print the remaining lines in the text file which were the last5. Which was the way it was intended to work. However, opening the SD card on my laptop and opening “test.txt” I saw the problem.
By putting “myFile.write(127);” I wasn’t deleting the actual characters at each cursor position, but REPLACING the original characters with DEL characters which on a Serial Monitor show up as nothing because DEL has no actual symbol so the numbers of original characters remained the same
In hindsight this is a dumb mistake on my part. I should have realized this as I saw on the ASCII reference table I was using that DEL indeed had no symbol (aka nothing), but I thought this meant being replaced with nothing, hence deleting the text!!
SO, for anyone who might make the same mistake as me, I hope this saves you time and effort.
However, that being said . . . does anyone know a method on how to actually delete the first line in a .txt file on an SD card using the Spark Core? Workarounds less needed, but will considering of course!
As always any direction is very much appreciated
Cheers,
UST