I seem to have a situation where rewindDirectory(); doesn't.
On the first pass, my code gives a complete list of files in the root.
Second pass, none are shown, despite calling close after openNext(); and calling rewindDirectory(); before listing the files.
Is there some conflict with rewindDirectory() if there are other files still open?
I'll post a test routine shortly...
OK, this code works in a simple test sketch but only works once in my main sketch...
void list_files2()
{
File sd_dir;
File fn;
unsigned char eof;
sd_dir = SD.open("/");
eof=0;
sd_dir.rewindDirectory();
while(!eof)
{
fn = sd_dir.openNextFile();
if (!fn)
{
// no more files
Serial.print("\n\rEND\n\r");
eof=1;
}
else
{
Serial.print(fn.name());
Serial.println(fn.size());
fn.close();
}
}
}
So any ideas what circumstances that can stop rewindDirectory() working?
You need to post the code that doesn't work. We can't guess what you are doing.
rewindDirectory() is a simple wrapper for the SdFat rewind function.
Here is rewindDirectory:
void File::rewindDirectory(void) {
if (isDirectory())
_file->rewind();
}
Here is the SdFat rewind function:
void rewind(void) {
curPosition_ = curCluster_ = 0;
}
These are very simple functions so we need more info about your code.
That's the code I am using, In one sketch the function works in another sketch it doesn't (it only works once).
I'm not able to post the entire sketch, I have pasted the function as is though.
My question is what could cause rewindDrirectory() not to work?
Will having other files open stop it working correctly?
My question is what could cause rewindDrirectory() not to work?
If you call rewindDirectory with a file that is not an open directory, it will do nothing.
Put a test in your program to make sure the open works.
if (!sd_dir) Serial.println("open failed for sd_dir");
You should close sd_dir before opening it again.
Thanks fat16lib.
I found the issue.
I had another file list routine that was filtering out specific filenames.
The file close(); you have to have with openNextFile(); was only called on the matching files so it was running out of file handles on the second pass...
I think the arduino SD reference example also omits the file close(); call too.
Nasty little bug but sorted now....
Thanks all.
I think the arduino SD reference example also omits the file close(); call too.
For years I have been pointing this out but the Arduino company is slow to fix things.