Go Down

Topic: How to open correct file in a list of files using "name + number" in filename (Read 512 times) previous topic - next topic

JO3RI

Having multiple files on an SD card, I can't seem to figure out how to open the correct file. Let's say I have 256 files like this:

Code: [Select]
ajob1.txt
ajob2.txt
ajob3.txt
...
ajob256.txt


Now the number is the ID I get from somewhere else:

byte ID = x
for (int x = 1; x < 257; x++)
  {
  }
then my filename is something like ajob ID .txt

but how do I get to replace the ID by the number so I can use this

Code: [Select]
myFile = SD.open("ajob ID .txt");
  if (myFile) {
    Serial.println("ajob ID .txt:");
   
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
    Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();



PaulS

Code: [Select]
int jobNumber =14;
char fileName[12];
sprintf(fileName, "ajob%d.txt", jobNumber);


will result in fileName containing "ajob14.txt", which can be used as an argument to SD.open().

Go Up