Hello! I hope very much for your help, as this is my diploma work, and time is running out.
And so, I have following code here (this is a fragment, but it has everything necessary):
#include <SPI.h
#include <SD.h>
String def_filename = "FILE"; //Standart name of files
String filename = "";
int slots_count = 20; //count of files
int filesrows[20]; //array for counters of rows in the files
File workFile;
void setup() {
if (!SD.begin(10)) {
Serial.println("initialization failed!");
return;
}
upd_filesrows();
}
void upd_filesrows() {
for(int i = 1 ; i <= slots_count; i +=1) { //scan for all files
filename = def_filename + i; //combine the name and ID (FILE1, FILE2..)
char temp[filename.length()+1];
filename.toCharArray(temp, sizeof(temp)); //convert to the char array (need for SD.open)
if (!SD.exists(temp)) { //If on the SD cart no file
workFile = SD.open(temp, FILE_WRITE); //create blank file
workFile.close();
filesrows[i-1] = 0; //count of rows for this file is 0
} else {
workFile = SD.open(temp); //Open existed file, and
counter = 0;
while (workFile.available()) {
char sd_t = workFile.read(); //read all chars,
if (sd_t == '\n') counter++; //if char = newline, increase the counter
}
workFile.close();
filesrows[i-1] = counter; //put counter to the array
}
filename = "";
}
}
It works. At the first start it creates an empty files. If the files already exist, it counts the number of rows and stores them in an array.
The problem is that, if upd_filesrows void is called from somewere in the code a second time, it writes to the array only zeros instead of the number of rows. Even if you just run this VOID twice, after the second time in the array will only zeros.
What's wrong? How is it possible to solve this problem? I tried to remove the file creation function, but it did not help.
Thanks in advance!