4

我正在用 C++ 制作一个 OpenGL 游戏。与其他语言相比,我在 C++ 方面相当缺乏经验。无论如何,我为一些图像创建了一个带有“基本”目录的字符串流。然后我将此字符串流作为函数参数传递给构造函数。构造函数附加一个图像文件名,然后尝试加载生成的路径。然而...

D:\CodeBlocks Projects\SnakeRoid\bin\Debug\Texts\ <-- before appending the filename
Ship01.tgacks Projects\SnakeRoid\bin\Debug\Texts\ <-- After.

显然不对!结果应该是 D:\CodeBlocks Projects\SnakeRoid\bin\Debug\Texts\Ship01.tga

我的代码的相关部分:

std::stringstream concat;
std::string txtFullPath = "Path here";

...

concat.str(""); //Reset value (because it was changed in ...)
concat << texFullPath; //Restore the base path
PS = new PlayerShip(&TexMan, concat); //Call the constructor

构造函数的代码

PlayerShip::PlayerShip(TextureManager * TexMan, std::stringstream &path)
{
    texId = 2;
    std::cout << path.str(); //First path above
    path << "Ship01.tga";
    std::cout  << path.str(); //Second - this is the messed up one
    //Do more fun stuff
}

任何人都知道为什么它“覆盖”字符串流中已经存在的内容?

4

1 回答 1

12

为什么它“覆盖”字符串流中已经存在的内容

因为输出将字符放在输出缓冲区中的“放置指针”位置。新构建的流的 put 指针设置为零(以附加模式打开的文件输出流除外),因此您的输出会覆盖缓冲区中已有的字符。

如果你真的需要以这种方式追加字符串,你需要将 put 指针移动到缓冲区的末尾:

std::cout << p.str(); //First path above
std::stringstream path;
path.str(p.str());
path.seekp(0, std::ios_base::end); // <-- add this
path << "Ship01.tga";
std::cout << "Loading player ship from " << path.str(); 

编辑:问题已被编辑,编辑后的代码有效,因为它不再用于path.str(p.str());在不使用输出操作(并且不推进 put 指针)的情况下创建输出缓冲区:请参阅ideone了解差异。

在任何情况下,字符串本身都可以连接起来,这将使代码更容易理解:

std::string p = path.str() + "Ship01.tga";
std::cout << p;

更不用说处理文件和路径名了,我们有boost.filesystem

于 2012-04-30T17:44:41.593 回答