为什么这段代码重复地只从字符串流中提取第一个字符串
Why is this code repeatedly extracting only the first string from the stringstream?
我正在一个实验室工作,该实验室需要解析文件中的字符串,以便用碎片填充游戏板。输入文件的格式如下:
black checker X 1 1
black checker X 2 0
red checker O 0 6
red checker O 1 5
下面是我从字符串流包装的tempString:中提取字符串的代码
int readGamePieces(std::ifstream & fileStream, std::vector<game_piece> & pieces, int widthBoard, int heightBoard) {
// attributes of the game piece being read from file
std::string color;
std::string name;
std::string display;
int xCoord = 0;
int yCoord = 0;
std::string tempString;
while (getline(fileStream, tempString)) {
std::cout << "getting new line" << std::endl;
std::cout << "contents of line: " << tempString << std::endl;
std::stringstream(tempString) >> color;
std::stringstream(tempString) >> name;
std::stringstream(tempString) >> display;
std::stringstream(tempString) >> xCoord;
std::stringstream(tempString) >> yCoord;
std::cout << "Game Piece Color: " << color << std::endl;
std::cout << "Game Piece Name: " << name << std::endl;
std::cout << "Game Piece Display: " << display << std::endl;
std::cout << "Game Piece xCoord: " << xCoord << std::endl;
std::cout << "Game Piece yCoord: " << yCoord << std::endl;
}
当我通过命令行运行这个程序时,我得到的输出如下:
getting new line
contents of line: black checker X 1 1
Game Piece Color: black
Game Piece Name: black
Game Piece Display: black
Game Piece xCoord: 0
Game Piece yCoord: 0
getting new line
contents of line: black checker X 2 0
Game Piece Color: black
Game Piece Name: black
Game Piece Display: black
Game Piece xCoord: 0
Game Piece yCoord: 0
getting new line
contents of line: red checker X 0 6
Game Piece Color: red
Game Piece Name: red
Game Piece Display: red
Game Piece xCoord: 0
Game Piece yCoord: 0
getting new line
contents of line: red checker X 1 5
Game Piece Color: red
Game Piece Name: red
Game Piece Display: red
Game Piece xCoord: 0
Game Piece yCoord: 0
是什么导致只重复提取字符串流中的第一个字符串?如何提取连续的字符串,直到行的末尾?
在每行中重新创建stringstream
实例:
std::stringstream(tempString) >> var1; // creates new stringstream instance
std::stringstream(tempString) >> var2; // creates new stringstream instance
std::stringstream(tempString) >> var3; // creates new stringstream instance
您应该使用局部变量来保留流的状态。我还用istringstream
替换了stringstream
,因为您只是在阅读流。
std::istringstream ss(tempString); // creates new stringstream instance just here
ss >> var1; // reads from the same stringstream
ss >> var2; // reads from the same stringstream
ss >> var3; // reads from the same stringstream
相关文章:
- 为什么它只打印双链接列表的第一个值,而我的程序却崩溃了
- std::find,返回所有找到的值的替代方法,而不仅仅是存在重复的向量的第一个值
- 如何仅读取文本文件中的第一个值
- 在C++中,如何在第一个"system()"结束后执行第二个"system()"?
- 查找不在标准中的第一个值::设置<int>最小-最大值
- C++:忽略第一个 cin.ignore 之后的输入
- C++:递归地将字符串中一个字母的所有实例替换为另一个字母
- 在C++中打印多个矢量的第一个值
- C++去除前x个元素的有效方法,在不改变向量大小的情况下将第x+1个元素推到第一个
- C++第一个cout将不会打印
- 我们可以在第一个else-if条件结束后使用另一个else-if条件吗
- OpenGL:第二个VBO破坏了第一个VBO
- 为什么第一个Dynamic_cast没有投射到基类?
- OpenGL 2D游戏只绘制第二个精灵纹理而不是第一个
- C++ 为什么程序只读取第一个值
- 使用C 中字符串标头的getline()函数将第一个char存储在字符串对象中
- 在将字符串转换为整数后,是否有一种方法可以保持数字的第一个零
- 将第一个boost::regex匹配放入字符串中
- 有一个字符串如何从第一个' /r/n/r/n '得到它的内容,直到2行从字符串结束
- 删除二进制字符串中的第一个0