Reading data from a Micro SD card.

I have an SD Micro SD Card module connected to an Arduino Nano.
I want to read specific lines from a .txt file to configure the arduino.
The lines should be read as a String with line endings in the file being "\n".
I have read other topics on this forum and a few others, but i did not understand them.
For communicating with the SD card i am using the SDFat library.
Does there exist a function like myFile.readline(lineNumber)?
If you could, please provide some example code.

Using (capital "S") Strings will cause the program to crash.

I have tried using 'string' but the code wont compile. what am i doing wrong?

HobbyMen:
I have tried using 'string' but the code wont compile. what am i doing wrong?

Without seeing your code?

How long are your lines? With an appropriately sized buffer, you can use readBytesUntil in a similar way as readStringUntil.

It does not solve your problem; you will need to do the counting yourself.

The lines are supposed to have data for a gsm module. They have 9 digit phonenumbers and specify pin modes(input,output). I will send the code here in a few minutes.

Here is the code i am currently using to find the indexes of the line endings to figure out where to look for data (podatoci means data, line endings are '|' with the line number):

#include <SPI.h>
#include "SdFat.h"
SdFat sd;
#define sd_CS_PIN SS
File myFile; 
String podatoci;
int index1;
int index2;
int index3;
void setup() {
  Serial.begin(9600);
  Serial.print("Initializing sd card...");
  if (!sd.begin(sd_CS_PIN)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  myFile = sd.open("TEXT.txt");
  if (myFile) {
    while (myFile.available()) {

      podatoci += myFile.readString();
    }
  }
  delay(250);
  Serial.println("Otcitani podatoci: " + podatoci);
  index1 = podatoci.indexOf("|1");
  index2 = podatoci.indexOf("|2");
  index3 = podatoci.indexOf("|3");
  Serial.println("Index1: " + index1);
  Serial.println("Index2: " + index2);
  Serial.println("Index3: " + index3);
}

void loop() {
  // put your main code here, to run repeatedly:

}

Here is the text in the .txt file( the data is currently different led pwm values to test the code )(Zolta means yellow, Sina means blue, Bela means white) :

Zolta180|1
Sina50|2
Bela255|3

Here is the serial monitor response( Otcitani podatoci means read data):

16:33:38.275 -> Otcitani podatoci: Zolta180|1
16:33:38.275 -> Sina50|2
16:33:38.275 -> Bela255|3
16:33:38.275 -> 
16:33:38.275 -> G⸮
16:33:38.275 -> ⸮

I don't use String (capital S) but I doubt that you can concatenate a text and a number like you do with e.g. Serial.println("Index1: " + index1);. That's probably why you get the garbage. Change that to

Serial.println("Index1: " + String(index1));

Have you tried readStringUntil instead of readString? That way you can read one line at a time and count them.

Below the use of so-called c-strings demonstrating a counting of lines

#include <SPI.h>
#include "SdFat.h"
SdFat sd;
#define sd_CS_PIN SS
File myFile;

char line[20];

void setup() {
  Serial.begin(9600);
  Serial.print("Initializing sd card...");
  if (!sd.begin(sd_CS_PIN)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  myFile = sd.open("TEXT.txt");
  if (myFile)
  {
    byte lineNumber = 0;
    while (myFile.available())
    {
      lineNumber++;

      // print line number
      Serial.print("line ");
      Serial.print(lineNumber);
      
      // fill the line variable with nul terminators
      memset(line, '\0',sizeof(line));
      byte numChars = myFile.readBytesUntil('\n', line, sizeof(line) -1);

      // print number of characters
      Serial.print(" contains ");
      Serial.print(numChars);
      Serial.print(" characters: '");

      // print line
      Serial.print(line);
      Serial.println("'");
      
    }
    myFile.close();
  }
  delay(250);
}

void loop()
{
}

sterretje:
I don't use String (capital S) but I doubt that you can concatenate a text and a number like you do with e.g. Serial.println("Index1: " + index1);. That's probably why you get the garbage. Change that to

Serial.println("Index1: " + String(index1));

This worked, thank you.

Have you tried readStringUntil instead of readString? That way you can read one line at a time and count them.

Below the use of so-called c-strings demonstrating a counting of lines

#include <SPI.h>

#include "SdFat.h"
SdFat sd;
#define sd_CS_PIN SS
File myFile;

char line[20];

void setup() {
  Serial.begin(9600);
  Serial.print("Initializing sd card...");
  if (!sd.begin(sd_CS_PIN)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  myFile = sd.open("TEXT.txt");
  if (myFile)
  {
    byte lineNumber = 0;
    while (myFile.available())
    {
      lineNumber++;

// print line number
      Serial.print("line ");
      Serial.print(lineNumber);
   
      // fill the line variable with nul terminators
      memset(line, '\0',sizeof(line));
      byte numChars = myFile.readBytesUntil('\n', line, sizeof(line) -1);

// print number of characters
      Serial.print(" contains ");
      Serial.print(numChars);
      Serial.print(" characters: '");

// print line
      Serial.print(line);
      Serial.println("'");
   
    }
    myFile.close();
  }
  delay(250);
}

void loop()
{
}

I want the arduino to work by finding the indexes of the line endings and based on the number on the line ending, to determine what it is, and where to look for it.
Thank you for your help! This has helped me a lot!