这段代码不断在我尝试检索的字符串前面投掷一个空格。C++ getline在字符串开头添加空格
void Texture_Manager::LoadSheet(std::string filename, std::string textfile)
{
std::ifstream infofile(textfile);
if (infofile.is_open())
{
std::string line;
while(std::getline(infofile, line))
{
std::string texturename;
sf::IntRect texture;
texture.height = 32; //these will be dynamic based on what the text file defines as the pixel sizes
texture.width = 32;
if(line.find("<name>") != std::string::npos)
{
std::size_t pos1 = line.find("<name>") + 6; //Search for the name of the texture
std::size_t pos2 = line.find(";", pos1);
std::size_t namesize = pos1 - pos2;
texturename = line.substr(pos1, namesize);
std::cout << texturename << std::endl;
}
}
}
这是我正在阅读的文件。我试图获得这个名字,它一直在沙漠和草地上放置一个空间。
<collection>tilemapsheet;
<ratio>32;
<name>desert; <coords>x=0 y=0;
<name>grass; <coords>x=32 y=0;
如果任何人有更好的建议,如何做到这一点以及我会很感激任何建设性的批评。我基本上搜索某个单词,然后读取信息以设置sfml中的纹理 – Joshua
您确定该空间没有被前面的“cout”调用输出吗?另外(不相关的),你应该把',pos1'放在你搜索的末尾;'''如果前面有一个' –
这是我的代码中的第一个cout调用。谢谢你的提示。它为沙漠和草地提供了一个空间。它在沙漠之后立即打印草而不会离开while循环。 – Joshua