Here is a simple program I made just trying to write a couple variables to a binary file, and then reading it back.
I'm trying to figure out how to read a string from a binary file. It wrote it correctly. But every time I try to read it(In this example it's 4 bytes), the result comes out really weird, which throws everything off. b = "jump", yet no matter how I try to read and convert, the end result turns out to something weird. And then variable b, which is read after it, also turns out weird.
What is the proper way to read a string from a binary file? All my strings are going to be fixed length anyways, 15 characters. Is there a way to do it without converting to a char array?
I also tried the .ReadCharArrays() method, and then toString(), and I'm still not reading the correct variables.
string path = ".//..//..//..//";
FileStream mfs = new FileStream(path + "test.bin", FileMode.OpenOrCreate, FileAccess.ReadWrite);
BinaryReader br = new BinaryReader(mfs);
BinaryWriter bw = new BinaryWriter(mfs);
short a = 2;
short b = 3;
string c = "JUMP";
bw.Write(a);
bw.Write(c);
bw.Write(b);
Console.WriteLine("Done writing to binary file");
br.BaseStream.Seek(0, SeekOrigin.Begin); //Apprantly you have to seek to beginning
a = br.ReadInt16();
c = br.ReadBytes(4).toString();
b = br.ReadInt16();
Console.WriteLine(""+a);
Console.WriteLine(""+c);
Console.WriteLine("");
Console.WriteLine("" +b);