Hi there..
I have txt file on SD card, the content of the file like this:
M2
# feep.uni
24 7
15
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 3 3 3 3 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 15 15 15 0
0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 15 0
0 3 3 3 0 0 0 7 7 7 0 0 0 11 11 11 0 0 0 15 15 15 15 0
0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 0 0
0 3 0 0 0 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
I need to skip the first two lines ( M2 and # feep.uni), and read the first, second, and third values (24 7 15) into a structure members ( int First; int Second; int Third;) .
sub->First = in_file.read(); //24
sub->Second = in_file.read(); //7
sub->Third = in_file.read(); //15
Then read the rest of values in 2D array.
The problem in this my code, I can not read to the values in correct way.
Can you help me to run my code correctly, please?
#include <stdio.h>
#include <stdlib.h>
#include <SD.h>
#define MAX 100
struct fileinfostructure
{
int Third;
int First;
int Second;
int data[MAX][MAX];
};
typedef struct fileinfostructure fileinfo;
void getfileinfo (char filename[], fileinfo *sub)
{
File in_file;
char ch;
int row, col, type;
int ch_int;
if (!SD.begin(BUILTIN_SDCARD)) {
Serial.println("Failed to mount card ");
}
in_file = SD.open(filename, FILE_READ);
if (!in_file) {
Serial.println("Opening file to read failed ");
}
ch = in_file.read();
if(ch != 'M')
{
Serial.print("ERROR(1): Not valid type\n");
exit(1);
}
in_file.read();
while(in_file.read() != '\n'); /* skip to end of line*/
while (ch = in_file.read() == '#') /* skip comment lines */
{
while (in_file.read() != '\n'); /* skip to end of comment line */
}
in_file.seek(in_file.position());
sub->First = in_file.read();
sub->Second = in_file.read();
sub->Third = in_file.read();
Serial.printf("\n width = %d",sub->First );
Serial.printf("\n height = %d",sub->Second );
Serial.printf("\n Third = %d",sub->Third);
for (row=0; row < (*sub).Second; row++){
for (col=0; col< (*sub).First; col++)
{
ch_int = in_file.read();
if (ch_int < 0)
Serial.printf("%d %d %d",row, col, ch_int);
(*sub).data[row][col] = ch_int;
}
}
in_file.close();
Serial.println("\nDone reading file.\n");
}
void setup() {
Serial.begin(9600);
while (!Serial){};
fileinfo *ss = (fileinfo*)malloc(sizeof(fileinfo));
getfileinfo ("datafile.txt", &(*ss));
}
void loop() {}