# SD Card read txt file specific data

#### imranahmed

Please let me know I want to read data from SD Card file name is driverID.txt , there are driver IDs in it.
2741#8785#3922#5805# like this.

I want to read 3922# but when I read it is removes data from start to some characters.

#### KlausST

##### Super Moderator
Staff member
Hi,

What's the problem?
SD card hardware?
File system?
Interface?
Microcontroller?
Software?
Anything else?

You are not the first one to read a SD card with a microcontroller. Thus there are many descriptions, examples code examples, application notes, videos...

Klaus

#### imranahmed

Hi,

What's the problem?
SD card hardware?
File system?
Interface?
Microcontroller?
Software?
Anything else?

You are not the first one to read a SD card with a microcontroller. Thus there are many descriptions, examples code examples, application notes, videos...

Klaus

Code attched I want to find specific driver number how is it possible?

Code:
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <SD.h>

File myFile;
String incomingString = "2741#";
String myString = "";
unsigned int myArrayIndex = 0;

LiquidCrystal_I2C lcd(0x27, 20, 4);

void setup() {
if (!SD.begin(10)) { //make sure sd card was found
while (true);
}

lcd.begin();
lcd.setCursor(0, 0);
lcd.print("DRIVERID.txt");
delay(2000);
lcd.clear();

}

void loop() {
myFile = SD.open("driverid.txt"); // open the file for reading
lcd.setCursor(0, 0); //
//lcd.autoscroll(); //enable auto-scrolling

if (myFile) {
while (myFile.available()) { //execute while file is available
if (letter == "2741#"){
lcd.print(letter);
}
//   delay(80);
}

myFile.close(); //close file
}

// lcd.clear();
}

#### Attachments

• DRIVERID.TXT
7.8 KB · Views: 2

#### andre_teprom

##### Super Moderator
Staff member
Still, no specific nor clear question.
Why not spent few more words to depict what exactly is working or not ?

#### imranahmed

Still, no specific question.
Why not sént few more words to depict what exactly is working or not ?

I want to read only 5805# in driverid.txt, but is it not working it is only read first driverid 2741# only.

#### andre_teprom

##### Super Moderator
Staff member
Ok, there is some advance in the question.
At least now we can presume that SD reading is working properly and the question is not specific to SD card reading itself.

Are you aware of the C standard String libraries ?
With these funcions you can find a text inside another.

#### imranahmed

Ok, there is some advance in the question.
At least now we can presume that SD reading is working properly and the question is not specific to SD card reading itself.
Are you aware of the C standard String libraries ?
Whith these funcions you can find text from another.
Yes SD Card working properly and question is not specific to SD card reading itself, it is reading good but I want to read specific driverid from file driverid.txt.

Yes I am aware of the C standard String libraries not all but most.
How brother please give hint, I am stuck.

#### andre_teprom

##### Super Moderator
Staff member
please give hint, I am stuck

There are many articles on the Web with comprehensive tutorials., why not start doing so ?
You have the above String variable "letter" within which you should seek for the keyword you need to find.

#### imranahmed

There are many articles on the Web with comprehensive tutorials., why not start doing so ?
You have the above String variable "letter" within which you should seek for the keyword you need to find.
I found many articles most of them using example of Arduino IDE of SD Card but I do not need it.
So what is the purpose of this forum if you recommend others to go to search on web, I was searched before asking on forum.

#### andre_teprom

##### Super Moderator
Staff member
I found many articles most of them using example of Arduino IDE of SD Card but I do not need it.
I said the question was no longer related to SD card, but rather to string manipulation
BTW, the purpose of this forum is people ask for help, providing as many information as possible and expect for tips.

I want to read only 5805# in driverid.txt, but is it not working it is only read first driverid 2741# only.

Since you did not seem able how to find the answer, consider doing that:

Replace this:
• if (letter == "2741#")

By this:
• if( letter .indexOf(" 5805#") > 0)

#### imranahmed

I said the question was no longer related to SD card, but rather to string manipulation
BTW, the purpose of this forum is people ask for help, providing as many information as possible as expect for tips.

Since you did not seem able how to find the answer, consider doing that:

Replace this:
if (letter == "2741#")

By this:
if( letter .indexOf(" 5805#") > 0)
Thank you I am trying it sorry I am not good in English but please try to understand.

#### andre_teprom

##### Super Moderator
Staff member
At the above snippet, remove the blank space, wrongly added on the beggining of the text of interest ( 5805#).
The same space before the 'dot' should be removed, apparently came with my copy-paste.

#### imranahmed

I said the question was no longer related to SD card, but rather to string manipulation
BTW, the purpose of this forum is people ask for help, providing as many information as possible and expect for tips.

Since you did not seem able how to find the answer, consider doing that:

Replace this:
• if (letter == "2741#")

By this:
• if( letter .indexOf(" 5805#") > 0)
My best brother thank you very much now I am successful because of your support thank you brother, from past 6 hours I was stuck. You are my bestd brother.

#### imranahmed

Hi,

Please let me know I wrote a code for reading data from SD Card but the function verify only runs first timer properly second time it is not working why?

Please find the attached Serial Monitor snap, only first time run works but after some delay again verification is not happening.
DriverID.txt file is attached.

#### Attachments

• DRIVERID.TXT
7.8 KB · Views: 4
• DataVerify.png
25.6 KB · Views: 6

#### andre_teprom

##### Super Moderator
Staff member
I wrote a code for reading data from SD Card but the function verify only runs first timer properly second time it is not working why?
No code, no guess...

imranahmed

### imranahmed

Points: 2

#### imranahmed

No code, no guess...

Ohh, Sorry I forgot.

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

bool verify = false;
String myString = "";
String type = "AZ-7773#";
String ID = "Vehicle ID";
File myFile;

bool verifydata(String typeID, String ID) {
Serial.print("SCAN DATA:");
Serial.println(typeID);
Serial.println(ID + ".txt below Scan Data ");
if (ID.equals("Driver ID"))
{
Serial.println("OPEN DRIVER ID");
SDcardinitialize();
myFile = SD.open("DRIVERID.txt");
}
else if(ID.equals("Vehicle ID"))
{
Serial.println("OPEN TRUCK ID");
SDcardinitialize();
myFile = SD.open("TRUCKID.txt");
}

if (myFile)
{
Serial.println(ID + ".txt in myFile");

// read from the file until there's nothing else in it:
while (myFile.available())
{
if (myString.indexOf(typeID) >= 0)
{
Serial.println("FOUND");
myString = "";
myFile.close();
return 1;
}
else
{
myString = "";
myFile.close();
return 0;
}

}
// close the file:
myFile.close();
}
else
{
// if the file didn't open, print an error:
Serial.println("error opening " + ID + ".txt");
}
}

void SDcardinitialize()
{
Serial.print("Initializing SD card...");

if (!SD.begin(10)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
}

void setup() {
Serial.begin(9600);
SDcardinitialize();
delay(2000);
}

void loop() {
verify = verifydata(type , ID);
if(verify == 0){
Serial.print("Not Verify ");
Serial.println(verify);
}else{
Serial.print("Verified ");
Serial.println(verify);
}
delay(4000);
}`

#### andre_teprom

##### Super Moderator
Staff member
At a glance, once you are repeatedly calling the SD.open() funtion, seems like you missed the SD.close() function somewhere.

### imranahmed

Points: 2

#### imranahmed

At a glance, once you are repeatedly calling the SD.open() funtion, seems like you missed the SD.close() function somewhere.
But I closed the file after reading in IF ELSE condition but is it not working, only first time running but after that showing un-expected behaviour.

#### andre_teprom

##### Super Moderator
Staff member
You close the file, however you cyclically instantiate the SD.begin() without having closed it. One thing is the SD card instance ( e.g: you could read more than one SD card in your application ), other thing is the file instance ( you could have several files in your card ). As said, seems like the last instance was not closed when you try to 'begin' another one after each delay.

#### imranahmed

You close the file, however you cyclically instantiate the SD.begin() without having closed it. One thing is the SD card instance ( e.g: you could read more than one SD card in your application ), other thing is the file instance ( you could have several files in your card ). As said, seems like the last instance was not closed when you try to 'begin' another one after each delay.
I tried many options but I do not know why it is not working, it is seems like no bugs but do not know why it is not working please ask to any expert.

Replies
0
Views
2K
Replies
2
Views
909
Replies
0
Views
986
Replies
7
Views
17K
Replies
1
Views
2K