Read RFID tag ref from SD card and check match

The RFID reader is a MFRC522 and using it with a Mega 2560. I have all the master tag references ( 4 byte wide ) stored in a SD card. When the user scans i get the scanned byte array in a readCard[] variable. And the code below is used for checking the match... since this is a function from a large program all variable declarations do not appear here.

Just wanted to know if there is a better way to do this comparison ??

// FUNCTION TO CHECK IF THE SCANNED RFID MATCHES WITH REQUIRED RFID

boolean rfidMatchDisp(unsigned long recIndex) {

  char rec_char_RFID[5];
  char act_char_RFID[5];
  rec_char_RFID[4] = '\0';                                 // Null Terminate the strings.... required ??
  act_char_RFID[4] = '\0';
  int result = -1;
  char letter ;
  char actualRFID[5];
  long rfidPosition = 0;

  File dataFile = SD.open("TAGREF.CSV", FILE_READ);        // Open the file
  dataFile.seek(recIndex + 1);                             // recIndex is from another function...
  
  rfidPosition = dataFile.position() + 1;                  // Index the RFID position for reading
  dataFile.seek(rfidPosition);
  for ( int i = 0; i < 4; i++ )  {                             // Read the four positions of RFID reference
    letter = dataFile.read();
    rec_char_RFID[i] = letter;
  }

   sprintf ( actualRFID, "%02X%02X%02X%02X", readCard[0], readCard[1], readCard[2], readCard[3]);  
  
  result = strcmp (rec_char_RFID, actualRFID);
 
  return result; 

  dataFile.close();
}

First of all, your sprintf will place 8 characters in actualRFID; problems guaranteed

A memcmp() might be a better option if the data is stored in binary format on the card; no conversions, just comparing 4 bytes.

  return result; 

  dataFile.close();

Don't you get a compiler warning because you have code that will never be reached?

johnwasser:

  return result; 

dataFile.close();




Don't you get a compiler warning because you have code that will never be reached?

Problem of selective posting to discuss a specific point. OK this is no defence for the goof up. Thanks for pointing out.

The compiler warning is set to Default level in my IDE 1.8.1 and maybe the warning scrolled up ....

I would compare each character as it is read from the file and stop as soon as there was a mismatch.

#include "SD.h"

void setup() {}
void loop() {}

char readCard[5];

boolean rfidMatchDisp(unsigned long recIndex) {

  int result = true;      // assume success
  long rfidPosition = 0;

  File dataFile = SD.open("TAGREF.CSV", FILE_READ);        // Open the file
  dataFile.seek(recIndex + 1);                             // recIndex is from another function...

  rfidPosition = dataFile.position() + 1;                  // Index the RFID position for reading
  dataFile.seek(rfidPosition);

  // look at each character
  for (int i = 0; i < 4; i++ )  {

    // if not a match, set return value and stop looking
    if ( dataFile.read() != readCard[i])
    {
      result = false;
      break;
    }
  }

  dataFile.close();

  return (result);
}

Looks so simple and functional and not using sprintf()

Exactly the kind of suggestion I was looking to learn. What is so difficult for a novice is basic for an expert. Let me check this out ..

Thanks for your time.