search for a specific line in a .txt file saved on a SD Card

Hello Guys!

I’ve got a SD card attached to my Arduino Uno and on that SD card there is a .txt file.
Inside of that .txt file there are numbers written (one number per line).
I now want to compare the value of an integer, with all lines of the .txt file, eg. search for a match.
And I actually have no idea how to approach this. :~

Thank you for your help! :slight_smile:

Step 1. Read each line from the file and display it on the serial monitor.

Ok done :slight_smile:
This code prints out all information inside the .txt file and displays it on the serial monitor.

void ListDatabase(){  
  Serial.println();
  RFIDFile = SD.open("Data.txt");
  if (RFIDFile) {
    while (RFIDFile.available()) {
      Serial.write(RFIDFile.read());
    }
    RFIDFile.close();         // close the file:
  } else {   	              // if the file didn't open, print an error
    Serial.println("error opening Data.txt");
  }
  return;
}

I believe now I have to somehow compare the lines of the serial monitor with the variable.

Step 1. Read each line from the file and display it on the serial monitor.

Ok done

Wrong. That is reading and printing each character. You are going to need to store the characters in an array until you encounter the end of record marker, and THEN print the line.

Ok,
but the thing is, that when I start the program, the .txt file will already contain some numbers, but the array will be empty.
How can I access the numbers inside the document and write them into the array? What would be the commands for that?

I know it must be possible somehow, the Arduino reference on SD says: "The File class allows for reading from and writing to individual files on the SD card." http://arduino.cc/de/Reference/SD

Use the read command you've already found. But rather than sending it char by char to serial, store it char by char in your array. Once you find an end of line marker ('\n'). null terminate the array, parse out the values and check whether it's the line you want.

You can try for this when in read mode.
myFile.find("923");
or
myFile.find(number_in_char_variable);

Works for me.
Cheers!