Recalculation of lines in a file on the SD card.

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!

Even if you just run this VOID twice

The term is FUNCTION, not VOID!

How is it possible to solve this problem?

Get rid of the String usage. It pisses away resources unnecessarily. sprintf() and a char array are MUCH better alternatives.

Add some debugging statements. Are you finding that the files exist, or not? Are you finding anything in the files?

this is a fragment, but it has everything necessary

No, it doesn't.

this is my diploma work, and time is running out.

Your problem for waiting so long to get started, not ours.

Thanks for you reply! I will try to debug it again and more detailed.