Pages: [1]   Go Down
Author Topic: SdFat lib and .exists()  (Read 471 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm having a problem with testing existing files on SD card.
For eg. I create "0.WPR", place the sdcard in arduino and then when I call getNewName(),
it always returns "0.WPR" which causes the same file to be reopened and overwritten.

The idea is to have multiple files named from 1 to max 999 with an WPR extensions.
Every file so far is properly written and closed before getNewName is called.

What could be the problem?

Code:
SdFat sd;
SdFile sdFile;
char fName[8];

void getNewName() {
int i = 0;

for (i = 0; i < 1000; i++) {
sprintf(fName, "%d", i);
strcat (fName, ".WPR");

if (!fileExists(fName)) {
break;
}
}

if (DEBUG_LEVEL >= 2) {
Serial.print("New name: ");
Serial.println(fName);
}

boolean fileExists(const char* name) {
return sd.exists(name);
}
}
Logged

Offline Offline
Faraday Member
**
Karma: 61
Posts: 2874
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well the first thing to do,  is check that fName is actually being properly formed.

The other thing that puzzles me,  is how you can access the function fileExists()  before you
declare it.   It is possible that you are actually getting some other function called fileExists() which
is not the one which you wrote.

I'm not too keen on nesting one function within another one.  I know you can do it,  but I don't remember
what the rules are,  so I never do it.

The last suggestion is,  if you can't even access the sd card at all,  then the call to sd.exists() will probably return
false,   which will cause the behaviour which you see.   Have you checked that you can actually "see" the sd
card at all ?   For example,  try running the example program from the playground which lists the files
on the sd card.

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm sorry - that was a bad paste, the function is declared before being called.
The problem seems to be with the SD module I have, pressing it against breaderbord solved the problem (after getting other strange errors like unable to init sd).
Logged

Pages: [1]   Go Up
Jump to: