user3087006
user3087006

Reputation: 9

Getline doesn't work for some reason

map - is a file that I opened. line - string

The upper part works, but the lower part does not.

    getline(map, line);
    getline(ssvalues, values, '|'); // Gets the name of the tileset file
    tileset.loadFromFile(values.c_str());
    getline(ssvalues, values, ' '); // Gets the size of the tile
    tileSize = atoi(values.c_str());

    getline(map, line); // Reads the next line.
    ssvalues.str(line);

    values = "";

    // FROM HERE IT DOESNT WORK, 'values' always empty, why--
    getline(ssvalues, values, '|'); // Get the X size of map
    std::cout<<values;
    mapSize.x = atoi(values.c_str());
    getline(ssvalues, values, ' '); // Get the Y size of map
    mapSize.y = atoi(values.c_str());
    std::cout<<values;

The content of the file that I'm reading is:

tileset.png|32
1200|1200

Upvotes: 0

Views: 155

Answers (1)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385405

ssvalues.str(line);

Here you reset the "contents" of the stringstream's buffer, but you didn't clear its error flags. Since it already hit EOF, this flag is still set and future getline calls will fail.

You should add error checking into your code for all these input operations, and write the following instead of the above:

ssvalues.clear();
ssvalues.str(line);

Upvotes: 2

Related Questions