How to find a string line in a txt file on SD-Card and replace a char... help!

Hi everyone, I’m new with Arduino and C language…
I’m trying the example of SDFat library (SdFatRemove.ino) that I’m trying to find a specific string line in the text file and replace the value after a comma “,”.

The text file format is like this:

07836883,x
13108266,x
13033706,x
13056789,x
13035732,x
13110781,x
07901021,x
18430841,x
18427551,x
16029778,x
18247387,x

And I’m trying to find the “13110781,x” in the file and replace the “x” value for anything / any char of my choice!

Here is the sketch:

/*
 * Rewrite Example
 *
 * This sketch shows how to rewrite part of a line in the middle 
 * of the file created by the SdFatAppend.pde example.
 * 
 * Check around line 30 of pass 50 of APPEND.TXT after running this sketch.
 */
#include <SdFat.h>
#include <SdFatUtil.h> // use functions to print strings from flash memory

const uint8_t SD_CHIP_SELECT = SS;

SdFat sd;

SdFile file;

// store error strings in flash to save RAM
#define error(s) sd.errorHalt_P(PSTR(s))

char buf[10];  // Buffer for read the Access file

void setup(void) {
  Serial.begin(9600);
  while (!Serial) {}  // wait for Leonardo
  PgmPrintln("Type any character to start");
  while (Serial.read() <= 0) {}
  delay(200);  // Catch Due reset problem
  
  // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  // breadboards.  use SPI_FULL_SPEED for better performance.
  if (!sd.begin(SD_CHIP_SELECT, SPI_HALF_SPEED)) sd.initErrorHalt();
  
  char name[] = "APPEND.TXT";
  // open for read and write
  if (!file.open(name, O_RDWR)) {
    PgmPrint("Can't open "); 
    Serial.println(name);
    PgmPrintln("Run the append example to create the file.");
    error("file.open failed");
  }
  // seek to middle of file
  //if (!file.seekSet(file.fileSize()/2)) error("file.seekSet failed");
  if (!file.seekSet(file.read(buf,12)) > 0) error("file.seekSet failed");
  if(strncmp(buf, "13110781", 8) == 0)
  {
    Serial.println("Match!");
    file.write("**Anything**");
  }
  // find end of line
  int16_t c;
  while ((c = file.read()) > 0 && c != '\n');
  if (c < 0) error("file.read failed");
  // clear write error flag
  file.writeError = false;
  // rewrite the begining of the line at the current position
  file.write("**OtherThing**");
  if (file.writeError) error("file.write failed");
  if (!file.close()) error("file.close failed");
  Serial.print(name);
  PgmPrintln(" rewrite done.");
}

void loop(void) {}

I would like to know how to use the “file.seekSet” properly for this job…
I get this on text file:

07836883,x
13108266,x
**OtherThing**056789,x
13035732,x
13110781,x
07901021,x
18430841,x
18427551,x
16029778,x
18247387,x

Instead of this:

07836883,x
13108266,x
13033706,x
13056789,x
13035732,x
13110781,**Anything**
07901021,x
18430841,x
18427551,x
16029778,x
18247387,x

Any help is very welcome!

seekset is useful if you are looking for a specific record in a file, the records are all exactly the same length, and you know which record you want. Yours are not. At least not after you get done.

Replacing ONE character with ONE other character is easy. That does not appear to be what you want to do, though.

PaulS: seekset is useful if you are looking for a specific record in a file, the records are all exactly the same length, and you know which record you want. Yours are not. At least not after you get done.

Replacing ONE character with ONE other character is easy. That does not appear to be what you want to do, though.

PaulS, thanks for the information! You're right ! Actually I would like to replace the "x" for "0" or some times for "1" like: "13110781,x" to "13110781,0" or "13110781,1". How can I get this working properly ?

Thanks again!

How can I get this working properly ?

You have to read every record, to determine if it is the correct one. Once you have the correct record, you can modify it. If you keep track of how many characters you have read when the file is opened for read, you can locate the start of the record easily, when the file is then opened for write.

PaulS:

How can I get this working properly ?

You have to read every record, to determine if it is the correct one. Once you have the correct record, you can modify it. If you keep track of how many characters you have read when the file is opened for read, you can locate the start of the record easily, when the file is then opened for write.

Thanks PaulS! I take a look on all arduino forum, and I can't find any example how to do this... Do you have some to share ?

Thanks again!

I take a look on all arduino forum, and I can't find any example how to do this...

To do what? To read each record? To compare the first n characters to another value? To determine the number of characters in the record? To add that to a running total, for later use in a seek? To modify a char array?

None of these tasks is at all complicated.

PaulS: Replacing ONE character with ONE other character is easy.

PaulS, does this involve rewriting the whole file to a temp file with the one character changed, deleting the orginal and renaming to temp back to the original? Or is there a faster way?

heinburgh: PaulS, does this involve rewriting the whole file to a temp file with the one character changed, deleting the orginal and renaming to temp back to the original? Or is there a faster way?

No, it involves having the file open for write.