【发布时间】:2013-03-31 00:53:03
【问题描述】:
我有一系列文件(New1.BMP、New2.BMP、...、New10.BMP)。
我需要创建一个变量来存储上述文件的名称,然后在另一个代码中使用它。
我当前的代码:
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));
}
如您所见,我不知道如何用 C++ 编写代码,我试图通过在线代码 sn-ps 来完成这项工作。这是它应该如何在 C/C++ 中工作的方式,即动态创建变量。
如何最好地处理它?
【问题讨论】:
-
查看
itoa的参数。在 C++ 中,这只是std::string name = "New" + std::to_string(l) + ".BMP"; -
变量是静态的东西。你不能像在解释语言中那样创建它们......
-
@Synxis 问题不在于创建具有动态名称的变量,而在于创建文件名。
-
C++ 是一种危险的编程语言。
-
“我不知道如何用 C++ 编写代码”。因此,在加入 SDL 之前,您应该建立一些基础。这个建议对每种语言都有效,尤其是 C++。