Jump to content
  • Advertisement
Sign in to follow this  
0x0539

[.net] C#'s istream equivalent

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

Hey I have a text file with ASCII encoded numbers in it (integers) and chars, and I would like to read them into variables similar to C++. What would be the C# equivalent to this C++ code? I want to keep it simple, and not have to read a line and parse it for the values. Data file:
1 2 3 a
Code:
int x, y, z;
char a;
ifstream text("file.txt", ifstream::in);
text >> x >> y >> z >> a;
ifstream.close();

Share this post


Link to post
Share on other sites
Advertisement
int x, y, z; char a;
using(var stream = new StreamReader(File.OpenRead("file.txt"))) {
var parts = stream.ReadLine.Split(new []{' '});
x = int.Parse(parts[0]);
y = int.Parse(parts[1]);
z = int.Parse(parts[2]);
a = parts[3][0];
}


Share this post


Link to post
Share on other sites
Wow, slap in the face lol. Thanks for the enthusiasm though. Now anyone have any ideas how to do this without parsing?

Share this post


Link to post
Share on other sites
Quote:
Original post by 0x0539
Wow, slap in the face lol. Thanks for the enthusiasm though. Now anyone have any ideas how to do this without parsing?

Sure, use an XML file format and serialization.

Share this post


Link to post
Share on other sites
Quote:
Original post by 0x0539
Wow, slap in the face lol. Thanks for the enthusiasm though. Now anyone have any ideas how to do this without parsing?


Well, being as istream parses and hides all the bloat away, your options are the two posted by Washu, or inherit from StreamReader to add ReadInt* etc functionality, either way, there's nothing OOTB that will do a one to one like istream.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!