Why two instance of File

Hi,
I have to rewrite a part of my code, concerning the SD card.
My goal is list the content of SD card.

If it does nit exiate, create a 2024 folder
CD the 2024 folder and if the month folder does not exist, create it
Cd the month folder and the day folder does not exist, create it

I look at this example file. I can see

File file;
File root;

then here, I can see

root.open("/")

and finally, here I can see

file.open("Folder1/file1.txt", O_WRONLY | O_CREAT)

What's the difference between root.open() and file.open() as both come from File?

EDIT: Another question come into my minde. Here you can read sd.chdir("Folder1")

What would be the difference between sd.chdir("/") and root.open("/"). In mine point of view sd.chdir("/") would be used to navigate into the SD, and root. and file. would be use to read a file, but how could you read "/"?

Hello pierrot10

Take a view here to get some ideas and gain the knowledge:

hth

They're two different instances of the File class. They're opening two different files.

It's due to the usage further down, in the middle of

  int rootFileCount = 0;
  if (!root.open("/")) {
    error("open root");
  }
  while (file.openNext(&root, O_RDONLY)) {
    if (!file.isHidden()) {
      rootFileCount++;
    }
    file.close();
    if (rootFileCount > 10) {
      error("Too many files in root. Please use an empty SD.");
    }
  }

file.openNext(&root) is a way to open the next file in the root directory, as file. Then it checks if there are too many visible (not hidden) files.

There may be other ways to do this with a single File instance, but this may be the best (efficient, easy, etc) for this particular file system API.

1 Like

Hello
Thanks for your replies