C ++-stringstream<<“覆盖" [英] C++ - stringstream << "overwriting"

查看:63
本文介绍了C ++-stringstream<<“覆盖"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C ++制作OpenGL游戏.与其他语言相比,我对C ++的了解还不够.无论如何,我为某些图像创建了一个带有基本"目录的字符串流.然后,我将此字符串流作为函数参数传递给构造函数.构造函数会附加一个图像文件名,然后尝试加载结果路径.但是...

I'm making an OpenGL game in C++. I'm fairly inexperinced in C++ as opposed to other languages. Anyway, I create a stringstream with the "base" directory for some images. I then pass this stringstream as a function parameter to a constructor. The constructor appends an image file name, then attempts to load the resulting path. However...

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

Obviously not correct! The result should be 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
}

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

Anyone have any idea why its "overwriting" what's already in the stringstream?

推荐答案

为什么要覆盖"?字符串流中已经有什么

why its "overwriting" what's already in the stringstream

因为输出将字符放置在输入指针"上,所以将其放置在输入指针"中.输出缓冲区中的位置.刚构造的流将put指针设置为零(除了在append模式下打开的文件输出流之外),因此您的输出将覆盖缓冲区中已经存在的字符.

Because output places characters at the "put pointer" position in the output buffer. A freshly-constructed stream has the put pointer set to zero (except for file output streams opened in append mode), thus your output overwrites the characters already in the buffer.

如果您确实需要以这种方式附加字符串,则需要将put指针移到缓冲区的末尾:

If you really need to append strings this way, you need to move the put pointer to the end of the buffer:

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()); 来创建输出缓冲区而不使用输出操作(且不提前放置放置指针):有关差异,请参见 ideone .

The question has been edited and the code after the edit works, because it no longer uses path.str(p.str()); to create the output buffer without using an output operation (and without advancing the put pointer): see ideone for differences.

在任何情况下,字符串本身都可以连接在一起,这将使代码更易于遵循:

In any case, strings themselves can be concatenated, which would make the code easier to follow:

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

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

Not to mention that for dealing with files and pathnames, we have boost.filesystem.

这篇关于C ++-stringstream&lt;&lt;“覆盖"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆