How to read txt file IN LINE from SD card?

Hi, I try to read file from SD card using arduino
I have Users.txt with content:
41001662
41001536
41001324

we store our Registration ID in that Users.txt file and whenever i register i must check it first in Users.txt first before i register him.
I can read all file, but I want to read line by line of Users.txt
How can I read 41001662 to data1, 41001365 to data2? read line by line?
Here is my code. How can I fix it?

#include <SD.h>
File myFile;
void setup()
{
Serial.begin(9600);
Serial.print(“Initializing SD card…”);
pinMode(10,OUTPUT);

if(!SD.begin(4))
{
Serial.println(“initialization failed!”);
return;
}
Serial.println(“initialization done.”);
myFile = SD.open(“login.txt”);
if(myFile)
{
Serial.println("login.txt: ");
while(myFile.available())
{
Serial.write(myFile.read());
}
myFile.close();
}
else
{
Serial.println(“error opening test.txt”);
}
}
void loop()
{

}

   while(myFile.available())
    {
      Serial.write(myFile.read());

You need to change this section of the code to put each character into an array of chars as it is read until you get a newline character. At that point you add a zero to the end of the array to make it into a C style string for use later.

The sticky 'serial input basics' near the top in this section might be of help. Depending on the source of the file, lines are terminated by the character '\r' (usually Mac), '\n' (Unix/Linux) or both (Dos/Windows).

How many records are there in the file? Each record is going to need 9 bytes of SRAM to store. You only have 2048 bytes (on most Arduinos).

UKHeliBob:

   while(myFile.available())

{
      Serial.write(myFile.read());



You need to change this section of the code to put each character into an array of chars as it is read until you get a newline character. At that point you add a zero to the end of the array to make it into a C style string for use later.

thats the problem, i dont know how to put each character into an array of chars.. will you please help me for that sir?. please thank you :smiley:

PaulS:
How many records are there in the file? Each record is going to need 9 bytes of SRAM to store. You only have 2048 bytes (on most Arduinos).

Good Morning sir. there will be many records in the txt file since it is where we store our registered Users. the ID is taken from the RFID card. so everytime we scan a new RFID card the ID of the card will store in Users.txt

As there is only limited memory in an Arduino and plenty records on the SD card, you need to read a record at a time and compare it against the entered ID. This can be a time consuming process if the ID that you want to compare is at the end of the file. You can speed up the process by always storing the IDs in alphanumerical order and using a specific algorithm to read the file (I'm not good in terminology but there is a name for it).

There is no difference between reading a file and reading from serial port. I did already refer to the sticky serial input basics; read it and apply the basics to your file reading.

You need an array of 9 characters; 8 for your databytes and a terminating null character. Read up to '\r' or '\n', replace that last character by '\0' and use strcmp() to compare.

The above assumes that the rfid is text based.

You can't go to a specific line per say, but what you can do is use the NewLine feed character "\n" to mark each line. Doing this, you can count the occurrences of the NewLine character and return the string before it.

You would probably need to write your own function to do this, but it's not impossible. Now to skip to a specific line, you just skip over x many newline characters until you get to the one you want.

Example: This is what it looks like in the txt file, only you don't see the \n at the end of each line, but it is there.

string1\n
string2\n
string3\n
. . .
stringN\n

You can't go to a specific line per say

You could, if you made sure that all records are always the same length.