XNA reading from files: ints turn into ASCII

Started by
1 comment, last by benryves 14 years, 2 months ago
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
Advertisement
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.
Quote:Original post by BosskIn Soviet Russia, you STFU WITH THOSE LAME JOKES!
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));

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

This topic is closed to new replies.

Advertisement