Jump to content
  • Advertisement
Sign in to follow this  
jfclavette

Best way to read from a stream detecting newlines

This topic is 4862 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

Here's a very noobish question. What's the best approach to read a file word by word but still have a way to know whether I've reached a new line and act accordingly ? On one hand, using istream::operator>>() would be great, but I don't have any way to detect if I skipped a space or a newline. On the other hand, I could use get line and tokenize the string, but this also look pretty messy to me. So yeah, what's the cleanest way to go about this ? It's not that I can't get it to work. it's just that it's a ~7 liner when it should be trivial.

Share this post


Link to post
Share on other sites
Advertisement
Not sure if i really do understand but oh well. I think there is no way to know where you are exactly in the. txt your reading in as far a i know.

Let's say you have a text file with


Aba Baba asda

Each time you do something like :

m_oFichierGeneration >> sEtat;

You read one of the string. Read char by char until it reach a blank space, and start reading after the blank space. This is how it detect the space.

What I'm saying is very noobish but well you said your question is :P

Edit : It automaticly detect the new lines.

Share this post


Link to post
Share on other sites
Quote:
Original post by deathwearer
Not sure if i really do understand but oh well. I think there is no way to know where you are exactly in the. txt your reading in as far a i know.

Let's say you have a text file with


Aba Baba asda

Each time you do something like :

m_oFichierGeneration >> sEtat;

You read one of the string. Read char by char until it reach a blank space, and start reading after the blank space. This is how it detect the space.

What I'm saying is very noobish but well you said your question is :P

Edit : It automaticly detect the new lines.


When you read this way, it gobbles the newline and doesn't tell you about it. I need to KNOW when it was a newline as opposed to a space or a tab.

Share this post


Link to post
Share on other sites
Quote:
Original post by jfclavette
On the other hand, I could use get line and tokenize the string, but this also look pretty messy to me.


That is how I'd do it.

Quote:
So yeah, what's the cleanest way to go about this ? It's not that I can't get it to work. it's just that it's a ~7 liner when it should be trivial.


C++ streams and strings aren't high-level enough for such things to be trivial.

In Python, you'd do something like: print [line.split() for line in myFile]

Share this post


Link to post
Share on other sites
Another option is to use getline() to read an entire line from the file, then dump the line into a stringstream and use operator>> to read the words from the line.

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!