YellowLT said:
spivonious said:
*snip*
I know how to use StreamWriter/Reader but writing to the file kills the format of the array which I'm trying to maintain in the file itself
In C#, because I'm feeling cruel to a VB newbie (though converting it to VB should be easy enough)
public static void Write2DArrayToFile(int[,] data, String fileName) {
using(FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write) {
// write out some useful header information
fs.Write( data.GetUpperBound(0) );
fs.Write( data.GetUpperBound(1) );
for(int x=0;x<=data.GetUpperBound(0);x++) {
for(int y=0;y<=data.GetUpperBound(1);y++) {
fs.Write( data[x,y] ); // as the data is fixed-size, you don't need to use any seperator characters like commas or tabs
}
}
}
}
and to read:
public static int[] Read2DArrayFromFile(String fileName) {
int[,] ret;
using(FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read)
using(BinaryReader rdr = new BinaryReader(fs)) {
int width = rdr.ReadInt32();
int height = rdr.ReadInt32();
ret = new int[ width, height ];
for(int x=0;x<width;x++) {
for(int y=0;y<height;y++) {
ret[x,y] = rdr.ReadInt32();
}
}
}
return ret;
}
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.