Opening a SD file with a variable

Quick question.

I have a routine that opens Help files that are stored on an SD card.

I currently access them with a lengthy switch case statement (There are 50+ .txt files).

How can I open an SD file using the variable of the help file I am currently accessing?

HelpPage is the variable 1-50 depending on the page I am opening.

The stored filenames are "Help1.txt", "Help2.txt", "Help3.txt", "Help4.txt" etc

E.G.

myFile = SD.open("HELP"+HelpPage+".txt");

Obviously this arrangement doesn't work, but wondered if it's possible?

Declare a static var fileName

char fileName[11]; // Helpxx.txt + '\0' = 11 char

set fileName with setFilename(n)

void setFilename(int n)
{
	if ((n<0) || (n>50))
        {
                // error, n out of range  
                fileName[0] = '\0';
		return;
        }
	
	sprintf(fileName,"Help%d.txt",n);
}

for example, to open “Help23.txt”

setFilename(23);

myFile = SD.open(fileName);

Thank you VLC. I was on the right lines, but I don't think I would have got there.
I will try that tomorrow.

if HelpPage is a byte or int then you need a cast

myFile = SD.open("HELP"+String(HelpPage)+".txt");

Many thanks
Worked a treat

if HelpPage is a byte or int then you need a cast

String(HelpPage) is NOT a cast.

Hi paul. here is a link to the official arduino page for converting ints to strings. i am aware that some of the serial print functions convert for you but wasnt aware of any casts in the SD library. if you have a better or more proficient way of casting I am always learning.

here is a link to the official arduino page for converting ints to strings.

Converting an int to a String is NOT the same as casting an int to a String. A cast says that the compiler should treat the bit pattern that is an int as though it was a String. An that is NOT a meaningful thing to do.

Creating a String that wraps a string that represents the int is something completely different.