openNextFile()
描述
開啟下一個檔案或是資料夾。
語法
file.openNextFile()
參數
file:被 SD.open() 回傳的 File 物件
回傳
File:如果檔案開啟成功回傳一個指向該檔案的 File 物件,如果開啟失敗則該 File 物件在使用 if 判斷時與布林值的 false 相等
範例
#include <SD.h>
File root;
void setup()
{
Serial.begin(9600);
SD.begin();
root = SD.open("/");
printDirectory(root, 0);
Serial.println("done!");
}
void loop()
{
// 不做任何事
}
void printDirectory(File dir, int numTabs) {
while(true) {
File entry = dir.openNextFile();
if (! entry) {
// 沒有未被開啟的檔案了
Serial.println("**no more files**");
}
for (uint8_t i=0; i<numTabs; i++) {
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println("/");
printDirectory(entry, numTabs+1);
} else {
// 如果是檔案會印出檔案大小
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}
See Also
- isDirectory()
- rewindDirectory()
The text of the 86Duino reference is a modification of the Arduino reference, and is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.
