How to read a data from SD card and compare it with a existing value

Hello,
I'm new to Arduino. I'm trying to read some data from the SD card module and do some action if that read data is equal to a string value. But with this code, its not happening. If anyone can please tell me a way to do that.

here is the code that I used.

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

File printFile;
String buffer;
boolean SDfound;


void setup() {
  Serial.begin(9600);
pinMode(10,OUTPUT);
 digitalWrite(10, HIGH);
  if (SDfound == 0) {
    if (!SD.begin(10)) {
      Serial.print("The SD card cannot be found");
      while(1);
    }
  }
  SDfound = 1;
 // Serial.print("The SD card  found");
  printFile = SD.open("test.txt");

  if (!printFile) {
    Serial.print("The text file cannot be opened");
    while(1);
  }

  while (printFile.available()) {
    buffer = printFile.readStringUntil('\n');
    Serial.println(buffer); //Printing for debugging purpose         
    if (buffer == "4652"){ // checking string
      Serial.println("found"); 
    }
  }

  printFile.close();
}

void loop() {
   //empty
}

you might want to trim() your String so that you get rid of possible \r or \n that would be part of what you read if (buffer.trim() == "4652")

using String is usually not well regarded for many reasons, but that can get you started

What output are you getting on Serial Monitor? Not much point in having all that debug output if you don't show us what you get. You can select the text in Serial Monitor and copy and paste it right to here.

Sorry for that johnwasser. Here is my serial output.

4587
7812
9675
4652
3458
4265

I want to check for that 4652 number in the output list and if it there, take some action.

Thank you J-M-L for the responding. But it gives an error. I cant understand that. Can you explain me please.

invalid operands of types 'void' and 'const char [7]' to binary 'operator=='

You may have a trailing '\r' remaining with readStringUntil('\n'). String.trim() will remove spaces, but not the trailing characters.

Rather than stripping the end characters and then making a text comparison to a number, it is more simple to convert the String from text to a number with String.toInt() and making an integer comparison.

while (printFile.available()) {
    buffer = printFile.readStringUntil('\n');
    Serial.println(buffer); //Printing for debugging purpose         
    if (buffer.toInt() == 4652){ // checking integer
      Serial.println("found");
    }

chamodmelaka:
Thank you J-M-L for the responding. But it gives an error. I cant understand that. Can you explain me please.

invalid operands of types 'void' and 'const char [7]' to binary 'operator=='

Sorry I don’t use the String class much - trim() is plain stupid, it does not return the modified string so you need to do it in two steps.

Try this

buffer.trim(); // get rid of unwanted spaces 
if (buffer == "4652") {...}

Thanks cattledog. It worked.

while (printFile.available()) {[color=#222222][/color]
    buffer = printFile.readStringUntil('\n');[color=#222222][/color]
    Serial.println(buffer); //Printing for debugging purpose         [color=#222222][/color]
    if (buffer.toInt() == 4652){ // checking integer[color=#222222][/color]
      Serial.println("found");[color=#222222][/color]
    }

Note that this works only for integers (besides 0).

if you want to recognize "0" and say you receive "Error" then as "Error" can't be parsed into an int, the the toInt() method returns 0

Also if you want to recognize a text string like "OK" or a floating point number "12.23" then it won't work.

It would be better for you to understand why it was not working in the first place (I suspect there is trailing carriage return '\r' that you can't see in the buffer)

Thank you all. Above both methods worked.