I assume you use this sketch (from Wokwi https://wokwi.com/projects/371869704522593281 )
#include <list>
#include <SD.h>
#define CS_PIN 5
File root;
std::list<String> indexList;
std::list<String> outList;
void setup() {
Serial.begin(115200);
Serial.print("Initializing SD card... ");
if (!SD.begin(CS_PIN)) {
Serial.println("Card initialization failed!");
while (true);
}
Serial.println("initialization done.\n");
createIndexList("FileB.txt");
evaluateFile("FileA.txt");
writeResultToFile("FileC.txt");
}
void loop() {
delay(100);
// nothing happens after setup finishes.
}
void createIndexList(String aFile){
indexList.clear();
String aLine = "";
File textFile = SD.open("/"+aFile);
if (textFile) {
Serial.println("Creating Index List from "+aFile);
while (textFile.available()) {
char c = textFile.read();
if (c >= ' ') {
aLine = aLine + c;
}
if (c == 10){
checkForIndexList(aLine);
aLine = "";
}
}
textFile.close();
if (aLine != "") {
checkForIndexList(aLine);
}
} else {
Serial.println("error opening "+aFile);
}
Serial.println();
showList();
}
void checkForIndexList(String line){
String idx = stripIndex(line);
if (idx > "") {
indexList.push_back(idx);
}
}
String stripIndex(String Line){
int c1 = Line.indexOf(',');
int c2 = Line.indexOf(',', c1+1);
String index = "";
if (c1 >= 0 && c2 > c1){
index = Line.substring(c1+1, c2);
}
return index;
}
void showList(){
Serial.println("===== Index List ========");
for (auto idx : indexList){
Serial.println(idx);
}
Serial.println("=========================\n");
}
void evaluateFile(String aFile){
String aLine = "";
outList.clear();
File textFile = SD.open("/"+aFile);
if (textFile) {
Serial.println("Evaluating "+aFile);
Serial.println("\n======= New Entries ========");
while (textFile.available()) {
char c = textFile.read();
if (c >= ' ') {
aLine = aLine + c;
}
if (c == 10){
checkForOutList(aLine);
aLine = "";
}
}
textFile.close();
checkForOutList(aLine);
} else {
Serial.println("error opening "+aFile);
}
Serial.println("============================\n");
}
void checkForOutList(String line){
String idx = stripIndex(line);
if (idx > "") {
if (!indexFound(idx)) {
Serial.println(idx);
outList.push_back(line);
}
}
}
void checkIndex(String line){
String idx = stripIndex(line);
if (idx > "") {
indexList.push_back(idx);
}
}
boolean indexFound(String aIndex){
boolean found = false;
for (auto ix : indexList){
if (aIndex == ix) found = true;
}
return found;
}
void writeResultToFile(String wFile){
File tFile = SD.open("/"+wFile, FILE_WRITE);
if (tFile) {
Serial.println("Writing to "+wFile);
Serial.println("\n========= Entries ========");
for (auto txt : outList){
tFile.println(txt);
Serial.println(txt);
}
tFile.close();
Serial.println("============================\n");
} else {
Serial.println("error opening "+wFile);
}
}
Additional Textfiles
FileA.txt:
0,B123,USUARIO TESTE,E5
1,X001,SERGIO FRANCA,F1
2,H002,LUCCA DA MATA,F2
3,M789,EMILLY MONTEIRO,F3
4,S111,JOANA FRANCA,EM1
FileB.txt
0,B123,USUARIO TESTE,E5,segunda-feira,31-07-2023,19:58:00
1,X001,SERGIO FRANCA,F1,segunda-feira,31-07-2023,19:58:00
2,H002,LUCCA DA MATA,F2,segunda-feira,31-07-2023,19:58:00
If yes - provided that you have copied the two files FileA.txt and FileB.txt to the SD Card- the expected Serial output is:
Initializing SD card... initialization done.
Creating Index List from FileB.txt
===== Index List ========
B123
X001
H002
=========================
Evaluating FileA.txt
======= New Entries ========
M789
S111
============================
Writing to FileC.txt
========= Entries ========
3,M789,EMILLY MONTEIRO,F3
4,S111,JOANA FRANCA,EM1
============================
Is something different or missing in your Serial output?
If yes, what is it?