Skip to content

Instantly share code, notes, and snippets.

@carlynorama
Last active November 2, 2022 13:33
Show Gist options
  • Save carlynorama/9300525 to your computer and use it in GitHub Desktop.
Save carlynorama/9300525 to your computer and use it in GitHub Desktop.
Creating a lot of files on an SD Card with an Arduino. Code complies. Keeping it as a reminder of how to work with String to char array conversion
void generateFiles() {
//derived from code found at http://forum.arduino.cc/index.php?topic=57460.0
String fileName = String();
String message = String();
unsigned int filenumber = 1;
while(!filenumber==0) {
fileName = "file_";
fileName += filenumber;
fileName += ".txt";
message = fileName;
char charFileName[fileName.length() + 1];
fileName.toCharArray(charFileName, sizeof(charFileName));
if (SD.exists(charFileName)) {
message += " exists.";
filenumber++;
}
else {
File dataFile = SD.open(charFileName, FILE_WRITE);
message += " created.";
dataFile.close();
filenumber = 0;
}
Serial.println(message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment