Go Down

Topic: sdfat - work with folders (Read 2294 times) previous topic - next topic

vvadim

I continue to study sdfat library. I can't find examples anywhere how to write down files in the separate folder, for example "statistics", and how to read them from it.
Thanks in advance.

cattledog

I'm not sure if this is what you want, but here's a sketch which creates a folder and writes files within it. I think the commands you are looking for are mkdir() and chdir(). make directory and change directory.

Code: [Select]
#include <SPI.h>
#include <SdFat.h>

const byte chipSelect = 10;
SdFat sd;
SdFile file;

void setup()
{
  Serial.begin(9600);

  Serial.print("Initializing SD card...");
  pinMode(10, OUTPUT);

  if (!sd.begin(10)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  //make Folders
  sd.mkdir("Folders");
  sd.chdir("/Folders");
  sd.mkdir("Folder_A");
  sd.mkdir("Folder_B");

  sd.chdir("/Folders/Folder_A");

  //open file within Folder
  file.open ("testA.txt", O_RDWR | O_CREAT | O_AT_END);
  Serial.print("Writing to Folder_A file testA.txt...");
  file.println("testing file write to Folder_A");
  // close the file:
  file.close();
  Serial.println("done.");

  sd.chdir("/Folders/Folder_B");

  file.open("testB.txt", O_RDWR | O_CREAT | O_AT_END);
  Serial.print("Writing to Folder_B file testB.txt...");
  file.println("testing file write to Folder_B");
  // close the file:
  file.close();
  Serial.println("done.");
  Serial.println();

  //reopen the files and read them

  sd.chdir("/Folders/Folder_A");
  file.open ("testA.txt", O_READ);
  Serial.println("Reading testA.txt");
  while (file.available()) {
    Serial.write(file.read());
  }
  // close the file:
  Serial.println();
  file.close();

  sd.chdir("/Folders/Folder_B");
  file.open("testB.txt", O_READ);
  Serial.println("Reading testB.txt");
  while (file.available()) {
    Serial.write(file.read());
  }
  // close the file:
  Serial.println();
  file.close();
}

void loop()
{}

vvadim


Go Up