Sign in to follow this  
MrLloyd1981

XNA reading from files: ints turn into ASCII

Recommended Posts

MrLloyd1981    126
Hi people, hopefully a really simple problem to solve. Im used to c++ style use of ifstreams and ofstreams and having problems getting my head around reading ints from a text file successfully in XNA. At the moment the values I'm reading in from a text file such as the following 10 5 16 12 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 0 0 0 0 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 Seem to be read in as ASCII values when going through the debugger. The relevent code that I'm using is
public Level(String name) 
        { 
            TYPE tileType; 
 
            System.IO.StreamReader levelFile = 
                new System.IO.StreamReader(name); 
 
 
 
            mPlayerPosition.X = levelFile.Read(); 
            mPlayerPosition.Y = levelFile.Read(); 
 
            mXSize = levelFile.Read(); 
            mYSize = levelFile.Read(); 
            . 
            . 
            . 
} 
I'm obviously going about something the wrong way, the 10 is being read as 52, or some similar value and so on. Please help! If this is the biggest boneheaded thing ever and there is a useful article on streams in c# for c++ zealots let me know and please share. Thanks

Share this post


Link to post
Share on other sites
grekster    640
The problem you have is that the text file just stores ascii data, and as far as I can tell the Stream class just gives you the raw data from the file. Im not a c# programmer but a quick google search gave me:

int.Parse(<string>)

which could be what you need to turn the ASCII data in the text file into actual numbers.

Share this post


Link to post
Share on other sites
benryves    1999
I'd personally be inclined to read a line at a time with StreamReader.ReadLine(), use string.Split(' ') to get the individual numbers as strings, then int.Parse() to turn each of these numeric strings into integers.

I'm quite fond of the Array.ConvertAll() method, which takes an input array and applies a user-defined converter to return a new array.

var line = levelFile.ReadLine();
var numbers = Array.ConvertAll<string, int>(line.Split(' '), int.Parse);
// alternatively, use a lambda:
var numbers = Array.ConvertAll(line.Split(' '), n => int.Parse(n));

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this