我有一个字符串数组
string name[1000];
与
int counter;
counter = 0;
while(FindNextFile(fHandle, &wf))
{
... //some more code which is checking if its a folder
string theName = wf.cFileName;
if(theName.find(".bmp") != std::string::npos)
{
name[counter] = theName;
counter++;
}
}
我正在将每个 .bmp 文件添加到我的名称数组中。
使用 NeHe's Tutorial我正在向我的 Qubes 添加纹理,效果很好。
现在我的自定义代码如下所示:
int n; string imageFileString[1000]; char *imageFile[1000];
for(n=0; n<1000; n++)
{
imageFileString[n] = name[n];
imageFile[n] = new char[imageFileString[n].length()];
strcpy(imageFile[n], imageFileString[n].c_str());
if(TextureImage[n] = loadBMP(imageFile[n]))
{
... // Some more Functions to set textures
}
}
一切正常,只是我的 BMP 文件没有加载。
如果我添加
name[0] = "pic1.bmp";
name[1] = "pic2.bmp";
name[2] = "pic2.bmp";
name[3] = "pic2.bmp";
在设置int n之前; string imageFileString...
,因此对于 for(...)
循环,我的图片会在不更改任何其他内容的情况下加载。我的第一个意见是名称数组没有条目,但我创建了一个包含输出的日志文件
name[0] << endl << name[1] << endl << name[2] ...
在我的日志文件中有相同的名字
pic1.bmp
pic2.bmp
pic3.bmp
所以我认为将 cFileName 添加到我的数组中还有一些其他错误。
谁能帮帮我?我不知道如何解决这个问题,我的意思是我不知道哪里出了问题...
最佳答案
imageFile[n] = new char[imageFileString[n].length()];
您没有考虑空终止符。长度加一:
imageFile[n] = new char[imageFileString[n].length() + 1];
关于C++ std::string to char 用于 OpenGL LoadBMP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4630390/