Jump to content
  • Advertisement
Sign in to follow this  
d000hg

C# equivalent to Scanner?

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

In Java, reading ints, floats, strings, etc from a stream is easy - SCanner.nextInt() for example. I just spent a few frustrating minutes looking to see an equivalent in C# - is there one or must I roll my own from Stream.Read()?

Share this post


Link to post
Share on other sites
Advertisement
Meh looking through all the base classes I must say that i indeed don't see anything of the sort. I think MS are pushing xml instead for plaintext files. But rolling your own is not to difficult, just a StreamReader.ReadLine() with one or two splits and a few <what ever basetype you want> parses.

Share this post


Link to post
Share on other sites
But streams aren't just files - what about reading from the console? It's not that easy to write your own - Java's expressions are really powerful, and even parsing a float isn't totally easy. I guess for reading ints and text it's easy enough though.

Share this post


Link to post
Share on other sites
For binary data, use BinaryReader. It reads data in little endian format. I don't know a good way to get big endian without subclassing BinaryReader.

For text data, you can use int.Parse(..), float.Parse(..), etc. I don't know offhand of a way to get the NextInt() functionality, but you could use stream.ReadLine(), and string.Split(..), then parse each result from Split.

Share this post


Link to post
Share on other sites
So for each string you get after splitting, you just try parsing as flaot,int, etc until one works? And if nothing does you assume it is just a string?

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!