Jump to content

C# - Skipping Bytes with BinaryReader?

I want to be able to, in a loop, read 4 bytes as an integer, skip the next 4, read the next 4 etc.

 

At the moment, I have this:

 

public bool checkHiScore()
        {
            string readPlayerNames; //Will hold the name for the entry being read in
            int readPlayerScores;   //Will hold the score for the entry being read in

            List<Tuple<string, int>> listHiScoreEntries = new List<Tuple<string, int>>();   /*Declares a Tuple list with two columns.
                                                                                              This will hold the name in one column 
                                                                                              and the score in the other*/

            using (BinaryReader binReader = new BinaryReader(File.Open(hiScorePath, FileMode.Open)))    //Creates a new instance of BinaryReader called binReader

                {
                    while (binReader.BaseStream.Position != binReader.BaseStream.Length)    //So long as binReader hasn't reached the end of the stream
                    {
                        readPlayerNames = binReader.ReadString();   //Sets the string currently being read in to readPlayerNames
                        readPlayerScores = binReader.ReadInt32();   //Sets the integer currently being read in to readPlayerScores

                        listHiScoreEntries.Add(new Tuple<string, int>(readPlayerNames, readPlayerScores));
                    }
                }

 

But I want to be able to replace reading in the string readPlayerNames with a line that will skip ahead 4 bytes. I googled this beforehand, but I couldn't find anything.

The biggest  BURNOUT  fanboy on this forum.

 

And probably the world.

Link to comment
https://linustechtips.com/topic/579574-c-skipping-bytes-with-binaryreader/
Share on other sites

Link to post
Share on other sites

What about binReader.ReadBytes(4) then throw away the result?

Aragorn (WS): 250D | 6800k | 840 Pro 512GB | Intel 530 480GB  | Asus X99-M WS | 64GB DDR4 | Corsair HX720i | GTX 1070 | Corsair H115i | Philips BDM4350UC 43" 3840x2160 IPS

Gimli (server):  Node 304 | G4560 | ADATA XPG SX8000 128GB | 2x 5TB WD Red | ASROCK H270M-ITX/AC  | 8GB DDR4 | Seasonic 400FL

 Omega (server):                 Fractal Arc Mini R2 | i3 4130 | 500GB Maxtor | 2TB WD Red : Raid 1 | 3TB Seagate Barracuda | 16GB RAM | Seasonic G-450w
Alpha (WS): 900D | 4770k | GTX 780  | 840 Pro 512GB  | GA-Z87X-OC | Corsair RM 850 | 24GB 2400mhz | Samsung S27B970D 2560x1440

                              ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

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

×