Read line by line from a sd card text file

Hey ,
This is my first post on the ARDUINO FORUM.
I’m kinda new to electronic programming. I’m most comfortable with but I can program in C# it just takes me longer , and I feel that arduino is very similar to C#.

I have a question and looked at the resources but couldn’t find anything related.

I want to be able to read a text file from a sd card (which I can already do) but do it line by line each time giving me a new string for the line , so I can test it against conditions. I’m using the dump file code that writes into the serial monitor.

const int chipSelect = 0;
#include <SD.h>
void setup()
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
  Serial.println("card initialized.");
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile ="datalog.txt");

  // if the file is available, write to it:
  if (dataFile) {
  while (dataFile.available()) {
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");

void loop()

But I am unsure how I would go about reading the text file line by line .
A solution would be much appreciated.

Thank you

allocate a string

write each char from the file into the string until you hit the new line character

at this point, do what you want with the string, which should contain an entire line

remember to null terminate appropriately, either once at the end, or once after each char of the file