Pages: [1]   Go Down
Author Topic: Using a variable file name?  (Read 1502 times)
0 Members and 1 Guest are viewing this topic.
San Diego, CA
Offline Offline
Karma: 0
Posts: 10
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have 10 files on the SD card 1.txt 2.txt 3.txt on up to 10.txt…

I want to have a variable I can increment in a loop that will count up such as:

// set up the variable to be used
Int FileToOpen = 1;

void loop() {

myFile = + ".txt");

// Do some reading of the file

// increment the file number to be opend


I get errors like cant use an int with a string/char and so on…

Any ideas of how to do this???

Thanks for any help!!!

Offline Offline
Edison Member
Karma: 35
Posts: 1057
Happy Hobbyist
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I asked same question here:,105997.0.html

As you se the solution was to use sprintf() something like

sprintf(newfile, "%0d.txt", FileToOpen); (your filenames should be 01.txt .. 10.txt)

« Last Edit: June 01, 2012, 01:24:53 pm by Erni » Logged

Manchester (England England)
Offline Offline
Brattain Member
Karma: 603
Posts: 33387
Solder is electric glue
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This might help, it is a function taken from my MIDI wave shield sample player. It generates a file name with a number only it uses .wav in place of the .txt that you want.
It builds up the name in an char array which is then passed to the file opening routine.

        char toPlay[11];     //  string array for file to play 00.WAV to 99999.WAV

void makeName(int number, int depth){  // generates a file name 0.WAV to 9999.WAV suppressing leading zeros
  if(number > 9) {
    makeName(number / 10, ++depth); // recursion
    number = number % 10;   // only have to deal with the next significant digit of the number
  toPlay[indexToWrite] = (number & 0xf) | 0x30;
  if(depth > 0) {return; } // return if we have more levels of recursion to go
  else {  // finish off the string with the wave extesion
  toPlay[indexToWrite] = '.';
  toPlay[1+indexToWrite] = 'W';
  toPlay[2+indexToWrite] = 'A';
  toPlay[3+indexToWrite] = 'V';
  toPlay[4+indexToWrite] = '\0'; // terminator
  indexToWrite = 0; // reset pointer for next time we enter

Pages: [1]   Go Up
Jump to: