Find the string in text file

Hello,

I have a doubt.
Is it possible to add a label in the text file which is stored in SD card and read from this label when want from the program.

parnal:
Hello,

I have a doubt.
Is it possible to add a label in the text file which is stored in SD card and read from this label when want from the program.

What do you mean by "add a label"? A label is just a piece of text. Adding text to a text file is easy.

What do you mean by "read from this label"? You read from a file, not from a piece of text.

Suppose, I have a program to find a String in the Text file.
When this String is found in the text file in SD Card, I want to read the content of this text file from this found String.

The program to find the string in the text file on SD Card:

#include <SD.h>

File myFile;
char buf[20];

void setup()
{
// Open serial communications and wait for port to open:
 Serial.begin(9600);
  while (!Serial) {
   ; // wait for serial port to connect
 }


 Serial.print("Initializing SD card...");
  pinMode(SDCARD_SS_PIN, OUTPUT);
  
 if (!SD.begin(SDCARD_SS_PIN)) {
   Serial.println("initialization failed!");
   return;
 }
 Serial.println("initialization done.");  
   
 // re-open the file for reading:
 myFile = SD.open("test1.txt");
 if (myFile) {
   Serial.println("test1.txt:");
   
   // read from the file until there's nothing else in it:
   
   while (myFile.available()) {


  myFile.read(buf,7);       
       if(strncmp(buf, "Readfromhere", 12) == 0)
       {
           Serial.println("Match!");

           //should read the content of files after this String
          break;     
       }
       
   }
   // close the file:
   myFile.close();
 } else {
  // if the file didn't open, print an error:
   Serial.println("error opening test.txt");
 }
}

void loop()
{
  // nothing happens after setup
}

The content of the text file looks like this :

HELLO
I am reading the file
Readfromhere
Line1
Line2

The result printed on the serial monitor after string found should be :

Line1
Line2
myFile.read(buf,7);

That would work great if your file is organised in blocks of 7 bytes. But what happens when you try to read this example file?

xReadfromhere

You get xReadfr, omehere.

Also, why are you trying to compare 12 characters in the buffer that was only filled with 7?

Hey Morgan,

was working on the other example with different text written in the file.
Obviously I will increase the buf size.

Hello,

The file finds the string written on the first line of the text file, but refuses to find the string written on the next line . How does this work?

#include <SD.h>

File myFile;
char buf[20];

void setup()
{
// Open serial communications and wait for port to open:
 Serial.begin(9600);
  while (!Serial) {
   ; // wait for serial port to connect
 }


 Serial.print("Initializing SD card...");
  pinMode(SDCARD_SS_PIN, OUTPUT);
  
 if (!SD.begin(SDCARD_SS_PIN)) {
   Serial.println("initialization failed!");
   return;
 }
 Serial.println("initialization done.");  
   
 // re-open the file for reading:
 myFile = SD.open("test.txt");
 if (myFile) {
   Serial.println("test.txt:");
   
   // read from the file until there's nothing else in it:
   
   while (myFile.available()) {


  myFile.read(buf,6);       
       if(strncmp(buf, "here", 4) == 0)
       {
           Serial.println("Match!");

           //should read the content of files after this String
          break;     
       }
       
   }
   // close the file:
   myFile.close();
 } else {
  // if the file didn't open, print an error:
   Serial.println("error opening test.txt");
 }
}

void loop()
{
  // nothing happens after setup
}

text file content:

Hello
here
read
write

The same program works fine if the string to be found is Hello but for here, read and write it doesnt work

parnal:
Hello,

The file finds the string written on the first line of the text file, but refuses to find the string written on the next line . How does this work?

#include <SD.h>

File myFile;
char buf[20];

void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
  while (!Serial) {
  ; // wait for serial port to connect
}

Serial.print(“Initializing SD card…”);
  pinMode(SDCARD_SS_PIN, OUTPUT);
 
if (!SD.begin(SDCARD_SS_PIN)) {
  Serial.println(“initialization failed!”);
  return;
}
Serial.println(“initialization done.”); 
 
// re-open the file for reading:
myFile = SD.open(“test.txt”);
if (myFile) {
  Serial.println(“test.txt:”);
 
  // read from the file until there’s nothing else in it:
 
  while (myFile.available()) {

myFile.read(buf,6);     
      if(strncmp(buf, “here”, 4) == 0)
      {
          Serial.println(“Match!”);

//should read the content of files after this String
          break;   
      }
     
  }
  // close the file:
  myFile.close();
} else {
  // if the file didn’t open, print an error:
  Serial.println(“error opening test.txt”);
}
}

void loop()
{
  // nothing happens after setup
}





text file content:


Hello
here
read
write




The same program works fine if the string to be found is Hello but for here, read and write it doesnt work

the content of buf after the first read is: H e l l o \n
the content of buf after the second read is: h e r e \n r
the content of buf after the third read is: e a d \n w r

this assume that \n denotes the linefeed, the content of buf is different if the line is terminated by \r\n

Hello,
How can this be fixed?
As I can see the string size when fixed to same length its easier to find it. But definitely this is not my case.

How can this be fixed?

I've got a tennis racket, a baseball bat, and a golf club. I'm going to hit you several times with the tennis racket, then once with the baseball bat and once with the golf club.

Can you tell which is which, when I hit you?

Well, you should be able to tell, when you read a character from the file, if it is a carriage return (baseball bat), a line feed (a golf club), or something else (useful, the tennis racket), and take the appropriate action - store the data, toss the data, or reset for processing the next record.

parnal:
Hello,
How can this be fixed?
As I can see the string size when fixed to same length its easier to find it. But definitely this is not my case.

Read blocks of 1, not 6.

Then look for the individual characters. Look for the 'h' first. If you found an 'h' you expect to see 'e'. If you don't find the 'e' go back to looking for the 'h'.

Hello all,

Thank you, It helped a lot to understand the problem.