Recalculation of lines in a file on the SD card.


hello! hope help, diploma work, , time running out.
and so, have following code here (this fragment, has necessary):

code: [select]
#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 counters of rows in files
file workfile;

void setup() {
if (!sd.begin(10)) {
serial.println("initialization failed!");
return;
}

upd_filesrows();
}

void upd_filesrows() {
for(int = 1 ; <= slots_count; +=1) { //scan files
filename = def_filename + i; //combine name , id (file1, file2..)
char temp[filename.length()+1];
filename.tochararray(temp, sizeof(temp)); //convert char array (need sd.open)
if (!sd.exists(temp)) { //if on sd cart no file
workfile = sd.open(temp, file_write); //create blank file
workfile.close();
filesrows[i-1] = 0; //count of rows file 0
} else {
workfile = sd.open(temp); //open existed file, and
counter = 0;
while (workfile.available()) {
char sd_t = workfile.read(); //read chars,
if (sd_t == '\n') counter++; //if char = newline, increase counter
}
workfile.close();
filesrows[i-1] = counter; //put counter array
}
filename = "";
}
}


it works. @ first start creates empty files. if files exist, counts number of rows , stores them in array.

the problem that, if upd_filesrows void called somewere in code second time, writes array zeros instead of number of rows. if run void twice, after second time in array zeros.

what's wrong? how possible solve problem? tried remove file creation function, did not help.

thanks in advance!

quote
even if run void twice

the term function, not void!

quote
how possible solve problem?

get rid of string usage. pisses away resources unnecessarily. sprintf() , char array better alternatives.

add debugging statements. finding files exist, or not? finding in files?

quote
this fragment, has necessary

no, doesn't.

quote
this diploma work, , time running out.

your problem waiting long started, not ours.


Arduino Forum > Using Arduino > Programming Questions > Recalculation of lines in a file on the SD card.


arduino

Comments

Popular posts from this blog

VIDIOC_S_FMT error 16, Device or resource busy - Raspberry Pi Forums

using a laptop skeleton to build a pi laptop - Raspberry Pi Forums

Forum for Joomla? - Joomla! Forum - community, help and support