C++ Arduino SD Fat库打开下一个错误
我正在使用Arduino SD fat库查找SD卡上的文件。我将向函数发送指向C++ Arduino SD Fat库打开下一个错误,c++,arduino,C++,Arduino,我正在使用Arduino SD fat库查找SD卡上的文件。我将向函数发送指向char数组的指针和对象SdFile作为模板,我的代码基于以下示例: const uint8_t SD_CS_PIN = 10; SdFat sd; SdFile file; SdFile dirFile; // Number of files found. uint16_t n = 0; // Max of ten files since files are selected with a single dig
char
数组的指针和对象SdFile
作为模板,我的代码基于以下示例:
const uint8_t SD_CS_PIN = 10;
SdFat sd;
SdFile file;
SdFile dirFile;
// Number of files found.
uint16_t n = 0;
// Max of ten files since files are selected with a single digit.
const uint16_t nMax = 10;
// Position of file's directory entry.
uint16_t dirIndex[nMax];
// List files in root directory.
while (file.openNext(&dirFile, O_READ))
{
// Skip directories and hidden files.
if (!file.isSubDir() && !file.isHidden())
{
// Save dirIndex of file in directory.
dirIndex[n] = file.dirIndex();
// Print the file number and name.
Serial.print(n++);
Serial.write(' ');
file.printName(&Serial);
Serial.println();
}
file.close();
}
下面是我编写的代码,当
loc.openNext(&dir,O_READ)
解释我的逻辑时,它不会返回true
开始
函数回显用户输入
bool FindFile (SdFile dir, char *findname)
{
Serial.println("getting file");
const char *p;
p = findname;
while (*p)
{
Serial.print(*p);
p++;
}
Serial.println();
Serial.println();
bool found = false;
检查文件名是否在根目录中
(本部分有效)
递归搜索
如果findname
不在dir
中,请查找第一个子目录,在那里进行检查并继续,直到找到或检查所有子目录。该函数还跳过任何隐藏的内容,因为我不希望用户能够选择隐藏的文件
else
{
//debug marker
Serial.println("else");
SdFile loc;
while (loc.openNext(&dir, O_READ))
{
//while loop is always returning false so this part is not running when it should
//debug marker
Serial.println("while");
if (loc.isHidden())
{
//file hidden, skip
}
else if (loc.isSubDir())
{
Serial.println("SUB");
loc.open(&dir, loc.dirIndex(), O_READ);
if(FindFile(loc, findname))
{
found = true;
break;
}
}
}
loc.close();
}
return found;
}
就我的一生而言,我无法理解file.openNext(&dirFile,O_-READ)
与loc.openNext(&dir,O_-READ)
编辑:更新代码以显示更多示例代码。其中是
dirIndex[n]=file.dirIndex()代码>dirIndex
已定义。如果这是在Arduino上,您知道RAM限制吗?更不用说在具有严重内存(例如堆栈)限制的设备上使用递归的潜在问题了。dirIndex[n]
是在上面的设置中定义的,更新后的帖子反映了这一点。我使用的Arduino Mega的文件数量相当有限(我看不出文件深度超过3-5个),所以这应该可以缓解递归的一些问题。如果有更好的方法,我会对学习感兴趣。dirIndex()
在SD Fat库中定义为在其目录中返回此文件的索引,其中是dirIndex[n]=file.dirIndex()代码>dirIndex
已定义。如果这是在Arduino上,您知道RAM限制吗?更不用说在具有严重内存(例如堆栈)限制的设备上使用递归的潜在问题了。dirIndex[n]
是在上面的设置中定义的,更新后的帖子反映了这一点。我使用的Arduino Mega的文件数量相当有限(我看不出文件深度超过3-5个),所以这应该可以缓解递归的一些问题。如果有更好的方法,我会有兴趣学习。dirIndex()
在SD Fat库中定义为在其目录中返回此文件的索引
else
{
//debug marker
Serial.println("else");
SdFile loc;
while (loc.openNext(&dir, O_READ))
{
//while loop is always returning false so this part is not running when it should
//debug marker
Serial.println("while");
if (loc.isHidden())
{
//file hidden, skip
}
else if (loc.isSubDir())
{
Serial.println("SUB");
loc.open(&dir, loc.dirIndex(), O_READ);
if(FindFile(loc, findname))
{
found = true;
break;
}
}
}
loc.close();
}
return found;
}