• 12
• 12
• 9
• 10
• 13

Should be an easy fix but it's driving me crazy.

This topic is 3892 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I'm trying to read in data for my games blocks from a text file. With the exception of the description of each block everything is numbers. The problem is that I need the description to contain spaces but using mapIn>>descrip quits on hitting a space. I know getline is going to come in somewhere but no matter how I try it it fails. and results in an empty string. Here is the code. It works perfectly as long as there are no spaces in the description: ushort mapHeight, mapWidth; ifstream mapIn("defaultMap.txt"); mapIn>>mapHeight>>mapWidth; i=0; while (i<(mapWidth*mapHeight)){ ushort x; ushort y; ushort walk; ushort chest; ushort enemy; string descrip; mapIn>>x>>y>>walk>>chest>>enemy>>descrip; block tempBlock(x,y,walk,chest,enemy,descrip); map.push_back(tempBlock); i++; }

Share on other sites
std::getline(mapIn, descrip);

Share on other sites
Can you post a sample text file so we can see exactly how they are formatted?

I'd guess that you want something like this:
mapIn>>x>>y>>walk>>chest>>enemy;std::getline(mapIn, descrip); //read the rest of the line into descrip

Share on other sites
I tried it that way and it didn't work. Here's a sample of the text:

3 //first two lines are mapHeight and mapWidth
3
0
0
1
1
1
path //the line in question

*note: the comments aren't in the file.

Share on other sites
Quote:
 Original post by RyutatsuI tried it that way and it didn't work. Here's a sample of the text:3 //first two lines are mapHeight and mapWidth300111path //the line in question*note: the comments aren't in the file.

Reading in a number with operator>> skips past *leading* whitespace, reads the number (if possible), and leaves any *trailing* whitespace behind.

std::getline reads from the current point until it sees a newline. The newline character is a kind of whitespace, so after you read the last number, the newline immediately after it is what gets seen by std::getline.

There is a way around this: reading into a special object*, std::ws, with operator>>, has the effect of skipping whitespace at the current point in the file. (It doesn't make sense to classify this as either "leading" or "trailing" whitespace, because there is no non-whitespace character being read.) In your case, you would simply do that after the last number.

Although this will screw you over if you want a description that has spaces or tabs at the beginning of it ;)

A more robust way of handling text files that have "line at a time" meaning is to read every line with std::getline, and re-parse each line with a std::stringstream object.

* std::ws is not *actually* an object, i.e. instance of a class. It's a function, which could also be called upon an ostream. The same is true of std::endl. The stream overloads operator>> so that "reading into" the right kind of function - called a "stream manipulator" - has the effect of calling the function on the stream.