Need help reading binary file

tsmith2471

Newbie
Joined
Oct 21, 2012
Messages
14
Reaction score
4
Trying to read a binary file and cannot put the bytes into a string. Does anybody have and advice? Thanks
Code:
 BinaryReader binaryreader = new BinaryReader(File.OpenRead(file location));

            while(binaryreader.Read() > 0)
            {
                                
                    byte[] empIdBytes = new byte[3];
                    binaryreader.Read();
                    String empId = new String(empIdBytes);
                    byte[] empIndBytes = new byte[1];
                    binaryreader.Read();
                    String empInd = new String(empIndBytes);
 
This won't work unless the file contains only strings.
To read a binary file, the structure has to be know.

Code:
 using (BinaryReader bin = new BinaryReader(File.Open("test.dat", FileMode.Open)))
            {
                while (bin.BaseStream.Position < bin.BaseStream.Length)
                {
                    int test1 = bin.ReadInt32();
                    byte test2 = bin.ReadByte();
                    string test3 = bin.ReadString();
                }
            }
 
Back
Top