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?

8
  • 1
    Look at the parameters for itoa. In C++, this is simply std::string name = "New" + std::to_string(l) + ".BMP"; Commented Apr 8, 2013 at 19:48
  • Variables are static things. You cannot create them like in interpreted languages... Commented Apr 8, 2013 at 19:48
  • @Synxis The question is not about creating variables with dynamic names, but simply about making file names. Commented Apr 8, 2013 at 19:49
  • 2
    C++ is a dangerous language to program flippantly. Commented Apr 8, 2013 at 19:49
  • 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++. Commented Apr 8, 2013 at 19:58

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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 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.
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/

1 Comment

fixed -- edited to refer to the c++ reference. PS: The answer remains right, whether you like the link I chose or not.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.