2

I have a series of files ( New1.BMP, New2.BMP, ...,New10.BMP).

I need to create a variable that stores the name of the above files and then use it in another code.

My current code:

int LengthFiles =10;
char FilePrefix[100]="New";//This is actually passed to the function. Am changing it here for simplicity
char str[200],StrNumber[1];//str holds the file name in the end. StrNumber is used to convert number value to character
 SDL_Surface *DDBImage[9]; // This is a SDL (Simple DIrectMedia Library) declaration.
 for (int l=0;l<LengthFiles ;l++)
     {      
    itoa(l, StrNumber, 1);
    strcat(str,FilePrefix);strcat(str,StrNumber);strcat(str,".BMP");
    DDBImage[l]= SDL_DisplayFormat(SDL_LoadBMP(str));

     }

As you might see, I have no clue how to code in C++, I have tried to make this work from code snippets online. Is this how it is supposed to work in C/C++, i.e. on the fly creation of variables.

How do I best approach it?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Foal
  • 23
  • 1
  • 3
  • 1
    Look at the parameters for `itoa`. In C++, this is simply `std::string name = "New" + std::to_string(l) + ".BMP";` – chris Apr 08 '13 at 19:48
  • Variables are static things. You cannot create them like in interpreted languages... – Synxis Apr 08 '13 at 19:48
  • @Synxis The question is not about creating variables with dynamic names, but simply about making file names. – David Heffernan Apr 08 '13 at 19:49
  • 2
    C++ is a dangerous language to program flippantly. – le3th4x0rbot Apr 08 '13 at 19:49
  • In C, `sprintf(str, "New%d.BMP", l);`, in C++ a stringstream, I'd think. – Daniel Fischer Apr 08 '13 at 19:49
  • Also notice that you're beginning to concatenate on nothing in particular. You need to use `strcpy` or initialize the first character to 0. – chris Apr 08 '13 at 19:50
  • How are you going to "create a variable that stores the name of the above files and then use it in another code"? Are you just using it in another class? Or another application? This will determine how to do it. – Frecklefoot Apr 08 '13 at 19:52
  • 2
    "I have no clue how to code in C++". So you should, before jumping in to SDL, build some foundation. This advice is valid for every language, specially for C++. – talles Apr 08 '13 at 19:58

2 Answers2

6

Your original question title was a little mis-leading because all you actually want to do is concatenate a string and an integer.

In C++ you would probably do that with stringstream:

stringstream ss;
ss << "New" << l << ".bmp";

And then to get a string variable:

string filename = ss.str();

And finally to pass a C string to the SDL function use c_str():

SDL_LoadBMP(filename.c_str())

The declaration of DDBImage is wrong. You need an array of length 10, but you declared it to have length 9. If you made LengthFiles into a constant you could write SDL_Surface *DDBImage[LengthFiles] and so be sure that the array was the correct length.

The code might then look like this:

const int FileCount = 10;
SDL_Surface *DDBImage[FileCount];
for (int index=0; index<FileCount; index++)
{      
    stringstream ss;
    ss << "New" << index << ".bmp";
    string filename = ss.str();
    DDBImage[index] = SDL_DisplayFormat(SDL_LoadBMP(filename.c_str()));
}

If your file names really start with New1.bmp then you'll need to adjust the indexing:

ss << "New" << index+1 << ".bmp";

Finally, if you need to extend this to handle a variable number of files, determined at runtime, then you should use vector<*DDBImage> instead of a raw array. Using vector<> allows you to let the C++ standard library take care of low-level memory management for you. In fact any time you find yourself allocating memory when programming in C++ you should ask yourself if there is already some part of the standard library that will do it for you.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Wonderful!!, Thanks a bunch @David Heffernan. Much appreciated. Especially the new code you pasted. It also did away with the const char * conversion error I would get otherwise. – Foal Apr 08 '13 at 20:12
  • 1
    @Foal Good. If you are going to use C++, try and forget what you know about C and learn how to do things the C++ way. Your life will be so much easier. – David Heffernan Apr 08 '13 at 20:15
0

You could use a formatted string printf... roughly along the lines of

sprintf( str, "%s%d.BMP", prefix, fileNumber );

Have a look at http://www.cplusplus.com/reference/cstdio/sprintf/

K Scott Piel
  • 4,320
  • 14
  • 19
  • 2
    1. Don't link to linux.die.net. The kernel docs are at kernel.org. 2. Don't link to linux docs to give a reference to a C standard library function. 3. I'm not actually the downvoter FWIW! – David Heffernan Apr 08 '13 at 19:53
  • fixed -- edited to refer to the c++ reference. PS: The answer remains right, whether you like the link I chose or not. – K Scott Piel Apr 08 '13 at 19:55