Jump to content
  • Advertisement
Sign in to follow this  
Ryutatsu

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

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Ryutatsu
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.


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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!