我正在制作一个使用“graphics.h” header 的 C++ 程序/游戏,并且我正在尝试创建一个带有图 block 的 map 。共有 66 个图 block ,每个文件名都不同。我想将它们全部显示出来,而不必一遍又一遍地写几乎相同的行。
这是我目前所拥有的(伪代码):
filename = a + number + b;
readimagefile (filename, left, top, right, bottom);
其中 a 是“bg(”,后跟从 1 到 66 的数字,然后是 b,即“.bmp”。我希望文件名是这样的:“bg(number).bmp”。但是,我上面的内容显然是不正确的语法。
我该怎么做呢?预先感谢任何答案。
最佳答案
std::stringstream str;
str << a << number << b << ".bmp";
然后 str.str()
返回一个 c++ std::string 而 str.str().c_str()
返回一个 'c' 类型的字符串
关于c++ - 在 C++ 中使用变量创建部分文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10854514/