C++ std::string to char 用于 OpenGL LoadBMP

标签 c++ string opengl char

我有一个字符串数组

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/

相关文章:

c++ - 字符串常量之前的预期标识符

php - 异常的PHP字符串长度以及使用PHP搜索Elasticsearch时

c++ - 字符串赋值

opengl - glActiveTexture 还是 glActiveTextureARB?

c++ - 通过远程桌面运行 qt creator

c++ - Qt单元测试会在Qt Creator的“应用程序输出” Pane 中为Qt 4.8重复所有操作两次(但在Qt 5.2.2中不会)

c++ - 在 C++ 中将整数更改为 wstring

ruby - 如何使用 ruby​​ 正则表达式获取子字符串?

algorithm - 检测后处理 GLSL 着色器上的灯光位置

java - LWJGL 顶点着色器 gl_PointSize 不工作