我想在位图文件名中传递一个变量,但要加载位图,它需要一个 const char*。是否可以转换我的数组以加载位图文件,或者是否有任何其他解决方案可以解决我的问题?
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#include <cstdio>
#include <iostream>
int main(){
char buf[40], char_nr[10], png_file[10];
int int_number = 1010;
ALLEGRO_DISPLAY *display = nullptr; al_init();
ALLEGRO_BITMAP *image = nullptr; al_init_image_addon();
display = al_create_display(800,400);
strcpy(buf, "\"./images/");
snprintf(char_nr, sizeof(char_nr), "%d", int_number);
strcat(buf, char_nr);
strcpy(png_file, ".png\"");
strcat(buf, png_file);
image = al_load_bitmap(buf);//buf="./images/1010.png"
std::cout << buf;//for test purposes
al_clear_to_color(al_map_rgb(0,0,0));
al_draw_bitmap(image, 10, 10, 0);
al_flip_display();
al_rest(3.0);
}
我的程序运行后崩溃了。 (allegro.exe 已经停止工作...)
最佳答案
引号不属于您的缓冲区。
您的 char*
数组可以传递给需要 const char*
的方法。
关于c++ - 传递一个变量来加载位图文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24190010/