Why is this code repeatedly extracting only the first string from the stringstream?

I am working on a lab that requires parsing strings from a file in order to populate a game board with pieces. The input file is formatted like this:

black checker X 1 1
black checker X 2 0
red checker O 0 6
red checker O 1 5

Below is my code that extracts a string from the stringstream-wrapped 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;
}

When I run this program through the command line, I get output like this:

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

What is causing only the first string from the stringstream to be repeatedly extracted? And how can I extract consecutive strings until the end of line?

Answers


You recreate the stringstream instance in each line:

std::stringstream(tempString) >> var1;   // creates new stringstream instance
std::stringstream(tempString) >> var2;   // creates new stringstream instance
std::stringstream(tempString) >> var3;   // creates new stringstream instance

You should use a local variable instead to preserve the state of the stream. I also replaced stringstream with istringstream as you are just reading from the stream.

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

Need Your Help

Store vector result from a loop into matrix when the dimension in unkown in R

r matrix vector

I want to store the vector result from a loop into a matrix, but I do not know the number of rows. For example, the following code will illustrate what I want to do:

How to give apache permission to write to home directory?

php apache

My server is in /var/www/html I have a php script in /var/www/html/fileio_test/io_test.php

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.