Pages: [1]   Go Down
Author Topic: How to open correct file in a list of files using "name + number" in filename  (Read 473 times)
0 Members and 1 Guest are viewing this topic.
Hamme, Belgium
Offline Offline
Sr. Member
****
Karma: 4
Posts: 386
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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();

Logged


Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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().
Logged

Pages: [1]   Go Up
Jump to: