Read SD file but record not found 2

Dear Fellowes

I´m trying to get a record inside a .txt file that already wrote on my PC into the SD card.
Then compare it and if ok or not do something.
My “project” was taken somewhere into the net. Follow some advices , from this forum, to put it work, but in my country we say: “Old dunk don’t learn foreign languages”, in this case programming languages.

Of course I´m a “old” newbie with just a few experience in Arduino but I love do it, and when see something hapen before my eyes this “make my day”.
I hope finnaly put it work with your help.
I use an card adapter very cheap, but works fine with SD examples.
I think that SD file reading has some “difference” of Char string used to compare because when I use a not listed 5 char code in file, the result is the same.
Thanks in advance
Jose

#include <SPI.h>
#include <SD.h>
//CS-D4,MOSI-D11,MISO-D12,SCK-D13
const int chipSelect=4;
char inp[6] = "737E4"; //file item for test
char db[6]; // char file read
const byte nch=6;
char rch[nch];
char rc;
byte ndx=0;

void setup(){  
  Serial.begin(9600);         
  SD.begin();     
  if(!SD.begin(chipSelect)){
   Serial.println("Card Fail");    
   return;
  }
 File mf = SD.open("db.txt");
   while (mf.available() ) {   
   // ndx =0; // same result with in or !
  rc =(mf.read()); 
  if( rc != '\n') {
  rch[ndx] = rc;
  ndx++;  
  } 
  // Serial.print(rc); //print all records   
//continue; //same result with in or !  
}  
 mf.close();
  if ((inp[6])!=(rch[ndx])) { //tryed inp, rc but same result    
  Serial.println("!OK Tag"); //allways !OKtag and OKtag ???
  delay(100);
  }  
  if ((rc)==(inp));{ 
  Serial.println("OK Tag");
  delay(100);
 }
}                 
 void loop(void){ 
    }

An array with 6 elements does not have an element with index 6. Indices go from 0 to 5 in that case.

// edit
And to compare text, you can use strcmp

Dear Fellowes
Here I’m again. Make changes as sugested, but until now no positive results.
Please give me some help here.
I never suppose that this task was so difficult to me!

#include <SPI.h>
#include <SD.h>
//CS-D4,MOSI-D11,MISO-D12,SCK-D13
const int chipSelect=4;
//char inp[5] = "737E4"; //file item for test
String inp = "737E4";  //changed to test Str compare
char db[5]; // char file read
const byte nch=5;
char rch[nch];
char rc;
byte ndx=0;
String src; //str from file search to Str compare
//boolean equals;

void setup(){  
  Serial.begin(9600);         
  SD.begin();     
  if(!SD.begin(chipSelect)){
   Serial.println("Card Fail");    
   return;
  }
 File mf = SD.open("db.txt");
   while (mf.available() ) {   
   // ndx =0; // same result with in or !
  rc =(mf.read()); 
  if( rc != '\n') {
  rch[ndx] = rc;
  ndx++; 
   src = rc;
  } 
  
  // Serial.print(rc); //print all records   
//continue; //same result with in or !  
} //while 
 mf.close();
  if (src.compareTo(inp) < 0 ) { // ==, equals, strcmp as same result OKTag
    Serial.println("OK Tag");    //

basically you should have a buffer that is as long as the string you are searching for, read byte by byte your file and add a byte in the buffer until the buffer is full, test the content of that buffer against the searched string if found, you stop, if not found you go to the next byte (possibly shifting the content of the buffer or overwriting the “oldest” byte depending how you want to implement the buffer)

as side notes:

  • Don’t use the String class
  • Don’t use the standard SD.h library, best to move to SdFat

And to compare text, you can use strcmp

But, you use strcmp() ONLY to compare strings. That requires that you KNOW what a string is.

See your other damned post.

DearFellowes

Thanks for your ideas to help me.
About using Strings I’m aware to spare Arduino memory and bad results with using it.
So I use char strings where I still have some doubts.
Trying ideas to understand what is really been read from char strings I show you what I see with my code.
I have 8 records in db.txt file: 91C89, 737E4, 86690, 76F34, F952D, FA044, AF3CC and CB34E.
Now removing 4 code lines that are taking chars from SD file, change the serialread() and result are only five very strange records:
18774, 697F4, 92F04, FCC3E. They still have 5 chrs each but with this results I believe that’s almost impossible to found any positive compare.

So, in my doubty opinion, the issue is in this part of code, or worst, in all code filosophy.

One advice was, if I realy understud, to pick a byte once at a time and compare, but I’m not are prepared to do this.

Any other ideas? Please I really need them.

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

//MOSI-D11,MISO-D12,SCK-D13, CS-D4
const int chipSelect=4;

char inp[5] = "737E4"; //file record for test
char rc; //mf read char
char rch[5]; // from search to compare
char ndx;

void setup(){  
  Serial.begin(9600);         
  SD.begin();     
  if (!SD.begin(chipSelect)) {
   Serial.println("Card Fail");    
   return;
  }
 File mf = SD.open("db.txt");
   while (mf.available() ) {   
  //rc =(mf.read()); 
  
 // if( rc == '\n') { 
   // rch[ndx] = rc;
  //  ndx++;
 // }
 Serial.write(mf.read()); //  ! print any records and this are bad news
 //to understand where is the issue 
    
    
  } //while end
mf.close();
  if (rch[5] == inp[5]) { 
    Serial.println("OK Code");// allways OK even when wrong code used.              
  delay(100);
  }  
 else { 
  Serial.println("!OK Code");     
  delay(100);
 }
}                 
 void loop(){ 
    }
char inp[5] = "737E4"; //file record for test

The compiler can count. Better than you can, in fact. "737E4" is a 6 character string. A string is a NULL terminated array of chars. You have not allowed space for the NULL terminator.

char rch[5]; // from search to compare

This one is too small, too.

 Serial.write(mf.read()); //  ! print any records and this are bad news
 //to understand where is the issue

Why are you using write() to send ASCII data to the Serial Monitor app? What do you think print() is for?

91C89, 737E4, 86690, 76F34, F952D, FA044, AF3CC and CB34E

result are only five very strange records:
18774, 697F4, 92F04, FCC3E.

Isn’t this interesting?!

91C89 737E4 86690 76F34 F952D FA044 AF3CC CB34E
 1 8  7 7 4  6 9  7 F 4  9 2  F 0 4  F C  C 3 E

I wonder how you did that.

Pete

Dear Fellowes
First of all sorry for my poor english.
Today tired of to be scared by invisible Chars, I’m trying a new aproch to see a litle bit more inspite of my poor Arduino code knowledge.
So, with this code I can see the chars be written, compared and I hope, with your help, match and put it work fine.
I increment the char strings values to 9, by excess but I’m not shure that’s correct.
So please give me your help and if this way is better or not.

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

//MOSI-D11,MISO-D12,SCK-D13, CS-D4
const int chipSelect=4;
File mf;
char inp[9] = "91C89"; //file record for test
int rb=0; //read bytes
char tagC[9];
String buf;
char cod[9];

void setup(){  
  Serial.begin(9600);         
  SD.begin();     
  if (!SD.begin(chipSelect)) {
   Serial.println("Card Fail");    
   return;   
  }
  //Serial.println(inp); // to test
 File mf = SD.open("db.txt");
   while (mf.available() ) {   
  rb =mf.readBytesUntil(1, tagC, 7); //last # must be 7 
buf=tagC;
buf.toCharArray(cod,9);
Serial.println(cod); //shows column with all records and between them      
                   // ! OK Code all wrong wy?
                     
  if ((cod,9) == inp[9]) { // 
    Serial.println("OK Code");// allways ! OK                   
 delay(100);
 mf.close();
 return;
  }  
 else { 
 Serial.println("!OK Code");     
 delay(100);
 }
   }                 
 void loop(){ 
    }

Have you looked at the documentation for readBytesUntil()? It's time that you did so.

The first argument is a character. What character are you expecting to find as a terminator? Does your file contain any 1s? NOT '1's, that's a whole different character.

Dear Fellowes

Finally I solve this issue.
My problem was don't mesure the char strings and strings
So I found a String.length function and verify all. Then create another string with substring to became shorter and its OK.
Thank you.
Jose