How to append "?" on the first line of the text on sd card to mark it as deleted

I have 30 lines 5 character each saved on my sd card.
After arduino read the first line. I would like to append “?” on the first line
so that on the next loop the program will skip it and will start reading on the next line.

As of now it is doing. I tried to looked for an example seek() but I got no luck. Please help. Thank you so much.

1HOKE
2GLJR
3SKLS
?
?
?

What I want to do is

?1HOKE
2GLJR
3SKLS

This is the code I’am working right now.

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

File printFile;
String buffer;
String insert = “?”;
boolean SDfound;

void setup() {

}

void loop() {
Serial.begin(9600);

if (SDfound == 0) {
if (!SD.begin(4)) {
Serial.print(“The SD card cannot be found”);
while(1);
}
}
SDfound = 1;
printFile = SD.open(“test.txt”);

if (!printFile) {
Serial.print(“The text file cannot be opened”);
while(1);
}

while (printFile.available()) {

buffer = printFile.readStringUntil(’\n’);
if(buffer[0]=="?")
{
continue;
}

Serial.println(buffer); //Printing for debugging purpose
//do some action here
delay(2000);
// Serial.print(“Reading next line\n”);

}

printFile.close();

delay(2000);

printFile = SD.open(“test.txt”, FILE_WRITE );
if (printFile) {

printFile.println(insert);

printFile.close();
//Serial.print(insert);

}else{
Serial.println(“Could not open file (writing).”);

}

delay(2000); // wait for 5000ms
}

You are appending ? to the file, not to the line of data in the file

In order to append a ? to a line there needs to be space for it

Please post the code that produced the layout you gave above

Will each record be the same length in the file as in your example ?

UKHeliBob:
You are appending ? to the file, not to the line of data in the file

In order to append a ? to a line there needs to be space for it

Please post the code that produced the layout you gave above

Will each record be the same length in the file as in your example ?

Yes. Same length in the file. I have posted the codes on my post above.
If that is not the proper way to post a code please tell. I will correct it. Thank you.

You can't directly insert data into a file.If you really wanted to do that, you would need to make a copy of the file and add the extra character as you were making the new one. What you can do is overwrite characters already in the file so you could put a ? in as the first char on a line you have processed. Would that be ok?

wildbill:
You can't directly insert data into a file.If you really wanted to do that, you would need to make a copy of the file and add the extra character as you were making the new one. What you can do is overwrite characters already in the file so you could put a ? in as the first char on a line you have processed. Would that be ok?

Yes. But I need to delete the first file so that on the next loop. Arduino will read the next line. Can you please help me with the codes. I would really appreciate it. I just couldn't find a good example only so I can work on with.

How tolerant to the data format is whatever is reading the data from the SD card ?
For instance, suppose that each of the following
1HOKE
2GLJR
3SKLS
were followed by a space character (or some other character) unless/until that character was replaced by a question mark ?

What I have in mind is making room for the question mark in the file when the data is written then overwriting it with a question mark when required

I would use "unsigned long pos = file.position();" before the read to get the file position of the first character in the line. Then, when the desired line is found, use "file.seek(pos); file.write('?');" to replace the first character of the line with a '?'.