Good Day All, I am currently working on a project and it has proved more difficult to resolve than expected even as I tried to go through the series of likely solutions online. It is also pertinent to state that I am not to vast in C language.
I have a SD Card with two files:
SPECIMEN.txt
CONTROL.txt
The First File (SPECIMEN.txt) contains values written in this format from line 1 to 6:
Q
W
E
R
T
Y
The Second File (CONTROL.txt) contains values written in the same format but not excatly the same content:
Q
T
Y
R
W
E
I have been trying to check in the two files and compare the content of both files as per line and to output if the lines have the same characters or not. E.g, from above:
Line 1 (Q) of File 1(SPECIMEN.txt) is the same as Line 1 (Q) of File 2(CONTROL.txt)
Line 2 (W) of File 1(SPECIMEN.txt) is not same as Line 2 (T) of File 2(CONTROL.txt), etc
I would like to output the number of Lines that are the same after a quick comparison, such that my result would look somewhat like:
Found: "2 lines are the same after comparison"
Thanks for your help in advance, I appreciate your time and efforts and tips.
I have the following codes:
#include <SPI.h>
#include <SD.h>
File mySpecimen;
File myControl;
const int CS = 4; //CS
char myArray1[6];
char myArray2[6];
int i=0;
int j=0;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// CHECK IF CARD FAIL TO INITIALIZE
if (!SD.begin(CS)) {
Serial.println("initialization failed!");
return;
}
// END
// RE OPEN SD CARD AND SEARCH CONTENT OF SPECIMEN
File mySpecimen = SD.open("SPECIMEN.txt");
if (mySpecimen) {
for(int i=0; mySpecimen.available(); i++){
myArray1[i] = mySpecimen.read();
///Serial.println(myArray1[i]);
// RE OPEN SD CARD AND SEARCH CONTENT OF CONTROL
File myControl = SD.open("CONTROL.txt");
if (myControl) {
for(int j=0; myControl.available(); j++){
myArray2[j] = myControl.read();
///Serial.println(myArray2[j]);
if(myArray1[i]==myArray2[j]) { Serial.println("MATCH FOUND");}
else{Serial.println("NO MATCH FOUND");}
mySpecimen.close();
myControl.close();
}
}
}
}
else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
// END
void loop() {
// nothing happens after setup
}
First, you have already defined i and j globally. No need to define them again in the for loops.
Second, your code closed both files after the first pass through the i loop, so you're not reading all of the data in. It might make it easier to see this kind of thing if you reformat the code in the IDE using Ctrl-T.
Third, why not read the first file and then read the second file. If not, then read the two files "together" inside a single for loop.
I don't have an SD card available, so I can't test this, but give it a try.
#include <SPI.h>
#include <SD.h>
#define ELEMENTS 6
File mySpecimen;
File myControl;
const int CS = 4; //CS
char myArray1[ELEMENTS];
char myArray2[ELEMENTS];
int i = 0;
int j = 0;
void setup() {
int errorFlag = 0;
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// CHECK IF CARD FAIL TO INITIALIZE
if (!SD.begin(CS)) {
Serial.println("initialization failed!");
return;
}
// END
// RE OPEN SD CARD AND SEARCH CONTENT OF SPECIMEN
File mySpecimen = SD.open("SPECIMEN.txt");
if (mySpecimen) {
for (i = 0; mySpecimen.available(); i++) { // Read first file
myArray1[i] = mySpecimen.read();
///Serial.println(myArray1[i]);
}
mySpecimen.close();
} else {
Serial.println("Error opening SPECIMEN");
errorFlag = 1;
}
File myControl = SD.open("CONTROL.txt");
if (myControl) {
for (j = 0; myControl.available(); j++) { // Read second file
myArray2[j] = myControl.read();
///Serial.println(myArray2[j]);
}
myControl.close();
} else {
Serial.println("Error opening CONTROL");
errorFlag = 1;
}
if (errorFlag == 0) { // Both opened okay
for (i = 0; i < ELEMENTS; i++) { // Walk through and compare
if (myArray1[i] == myArray2[j]) {
Serial.println("MATCH FOUND");
}
else {
Serial.println("NO MATCH FOUND");
}
}
}
}
void loop() {
}
Works perfectly @econjack :). your code was ablle to detect a match, however, I used it to comapare two files with exactly the same content of 5 lines and it found a match, it would be nice to show the amount of times a match was found so as to know how many match left.
Result like:
// For 2 found matches
MATCH FOUND
MATCH FOUND
OR
Result like:
// For 2 found matches
2
I will try from my end to achieve the above result, hoping not to mess with the codes you sent.
Again, thanks for your time and assistance!
Modified: I observed that after the first time, it worked but every other sketch upload of the same code you posted, shows "Error Opening Control..." strange
I was able to get the SD Card to open 2 files and read from them with the code below but it does not compare the two files accurately. I would appreciate a tip or any assistance, thanks in advance.
#include <SPI.h>
#include <SD.h>
#define ELEMENTS 5
File mySpecimen;
File myControl;
const int CS = 4; //CS
char myArray1[ELEMENTS];
char myArray2[ELEMENTS];
int i = 0;
int j = 0;
int x = 0;
void setup() {
int errorFlag = 0;
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// CHECK IF CARD FAIL TO INITIALIZE
if (!SD.begin(CS)) {
Serial.println("initialization failed!");
return;
}
// END
// RE OPEN SD CARD AND DISPLAY CONTENT SPECIMEN
File mySpecimen = SD.open("SPECIMEN.txt");
if (mySpecimen) {
SPECIMEN:
for (int i = 0; mySpecimen.available(); i++) { // Read first file
myArray1[i] = mySpecimen.read();
Serial.println(myArray1[i]);
mySpecimen.flush();
Serial.flush();
if(i < ELEMENTS){goto SPECIMEN;}else{goto CONTROL;}
}
mySpecimen.close();
}
else {
// if the file didn't open, print an error:
Serial.println("Error opening SPECIMEN");
errorFlag = 1;
}
// RE OPEN SD CARD AND DISPLAY CONTENT CONTROL
CONTROL:
File myControl = SD.open("CONTROL.txt");
if (myControl) {
for (int j = 0; myControl.available(); j++) { // Read SECOND file
myArray2[j] = myControl.read();
Serial.println(myArray2[j]);
myControl.flush();
Serial.flush();
}
myControl.close();
}
else {
// if the file didn't open, print an error:
Serial.println("Error opening CONTROL");
errorFlag = 1;
}
if (errorFlag == 0) { // Both opened okay
for (x = 0; x < ELEMENTS; x++) { // Walk through and compare
if (myArray1[i] == myArray2[j])
{Serial.println("MATCH FOUND");
}
else {
Serial.println("NO MATCH FOUND");
}}
}
// END
void loop() {
// nothing happens after setup
}
First, get rid of the goto's; those are crutches and not needed. Second, why did you add the two flush() calls? Do you understand their purpose? Third, you chose not to reformat your code using Ctrl-C, so this mess:
if(i < ELEMENTS){goto SPECIMEN;}else{goto CONTROL;}
}
mySpecimen.close();
}
else {
// if the file didn't open, print an error:
Serial.println("Error opening SPECIMEN");
errorFlag = 1;
}
not only is more difficult to read, but your program won't even compile. ALWAYS use CTRL-T before you post any source code to the Forum. Had you done that, it would have been easier for you to spot your error.
Done sir, ive seen some of my mistakes and I've also reverted to the first solution code you posted and it compiled perfectly and fetched data from the SD Card without issues but the only thing I observed is that the comparison does not work.
#include <SPI.h>
#include <SD.h>
#define ELEMENTS 6
File mySpecimen;
File myControl;
const int CS = 4; //CS
char myArray1[ELEMENTS];
char myArray2[ELEMENTS];
int i = 0;
int j = 0;
void setup() {
int errorFlag = 0;
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// CHECK IF CARD FAIL TO INITIALIZE
if (!SD.begin(CS)) {
Serial.println("initialization failed!");
return;
}
// END
// RE OPEN SD CARD AND SEARCH CONTENT OF SPECIMEN
File mySpecimen = SD.open("SPECIMEN.txt");
if (mySpecimen) {
for (i = 0; mySpecimen.available(); i++) { // Read first file
myArray1[i] = mySpecimen.read();
Serial.println(myArray1[i]);
}
mySpecimen.close();
} else {
Serial.println("Error opening SPECIMEN");
errorFlag = 1;
}
File myControl = SD.open("CONTROL.txt");
if (myControl) {
for (j = 0; myControl.available(); j++) { // Read second file
myArray2[j] = myControl.read();
Serial.println(myArray2[j]);
}
myControl.close();
} else {
Serial.println("Error opening CONTROL");
errorFlag = 1;
}
if (errorFlag == 0) { // Both opened okay
for (i = 0; i < ELEMENTS; i++) { // Walk through and compare
if (myArray1[i] == myArray2[j]) {
Serial.println("MATCH FOUND");
}
else {
Serial.println("NO MATCH FOUND");
}
}
}
}
void loop() {
}
I observed that the problem might be from the excess space between the lines as seen on the serial monitor.
I was banned for 3 days for time wasting, so I'll just go straight to the point. Still on this issue, I have been able to read content of the two files from SD Card with the code below:
#include <SPI.h>
#include <SD.h>
#define ELEMENTS 6
File mySpecimen;
File myControl;
const int CS = 4; //CS
char myArray1[ELEMENTS];
char myArray2[ELEMENTS];
int i = 0;
int j = 0;
void setup() {
int errorFlag = 0;
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// CHECK IF CARD FAIL TO INITIALIZE
if (!SD.begin(CS)) {
Serial.println("initialization failed!");
return;
}
// END
// RE OPEN SD CARD AND SEARCH CONTENT OF SPECIMEN
File mySpecimen = SD.open("SPECIMEN.txt");
if (mySpecimen) {
for (i = 0; mySpecimen.available(); i++) { // Read first file
myArray1[i] = mySpecimen.read();
Serial.print(myArray1[i]);
}
mySpecimen.close();
} else {
Serial.println("Error opening SPECIMEN");
errorFlag = 1;
}
File myControl = SD.open("CONTROL.txt");
if (myControl) {
for (j = 0; myControl.available(); j++) { // Read second file
myArray2[j] = myControl.read();
Serial.print(myArray2[j]);
}
myControl.close();
} else {
Serial.println("Error opening CONTROL");
errorFlag = 1;
}
if (errorFlag == 0) { // Both opened okay
for (i = 0; i < ELEMENTS; i++) { // Walk through and compare
if (myArray1[i] == myArray2[j]) {
Serial.print("MATCH FOUND");
}
else {
Serial.print("NO MATCH FOUND");
}
}
}
}
void loop() {
}
Serial monitor now outputs without the excess line spaces, however, the output from serial monitor reads:
//Content of first file
Q
W
E
R
T
YA// Content of second file (note that "A" is not supposed to be on that line)
B
C
D
E
F
I was hoping to get:
//Content of first file
Q
W
E
R
T
Y
A// Content of second file
B
C
D
E
F
Perhaps, I would be able to compare the arrays of the two files correctly if I could get someone to point me to the right direction. Please I require your assistance.
Sir, I followed your direction changing from Println to Print and was able to at least get the project working to a reasonable state. in order for me to progress on this project, I would say my answer to your question in reply #7 is a capital NO. Kindly tell me what to do.
AWOL:
Put a println() after printing the contents of the first file.
The presentation of the data in the monitor has nothing to do with the comparison.
Ok, I just did and got this as output:
Q //first file
W
E
R
T
YA // second file
B
C
D
E
F
But as you said the representation given by the serial monitor has got nothing to do with the comparison, what might help solve this comparison problem.
I don't know by which thought process you arrived at that result, because you didn't share your code, but my thoughts went along these lines.
// RE OPEN SD CARD AND SEARCH CONTENT OF SPECIMEN
File mySpecimen = SD.open("SPECIMEN.txt");
if (mySpecimen) {
for (i = 0; mySpecimen.available(); i++) { // Read first file
myArray1[i] = mySpecimen.read();
Serial.print(myArray1[i]);
}
mySpecimen.close();
} else {
Serial.println("Error opening SPECIMEN");
errorFlag = 1;
}
// at this point, we've finished printing the first file's contents
Serial.println();
To be honest, I do not know how to implement it, I have however, googled and see what to do but, not really sure. I appreciate you taking your time to reply every of my post, thanks...Uffffffff! if you decide not to respond to this I'll totally understand...I will read on your suggestion, thanks again