[.net] Reading values stored in a file?

Started by
5 comments, last by nick5454 14 years, 8 months ago
I normally have no problem with this, but this particular problem has been bugging me for 2 days now. |: The following function can't read the 2D array in the file because Width and Height are being reported as being 41thousand-something and 2 respectively;
        /// <summary>
        /// Loads and returns a new Map instance from a given Flexible Level Data file.
        /// </summary>
        /// <param name="Path">The path to the file, including filename.</param>
        /// <returns>The new Map instance.</returns>
        public static Map FromFile(string Path)
        {
            BinaryReader Reader = new BinaryReader(File.Open(Path, FileMode.Open));
            ASCIIEncoding Encoding = new ASCIIEncoding();

            string Signature = Encoding.GetString(Reader.ReadBytes(3));

            if (Signature != "FLD")
                throw new Exception("Tried loading a non-leveldata file in Map.FromFile()!");

            int Version = Reader.ReadInt32();

            if (Version != 1)
                throw new Exception("Tried loading FLD file with wrong version in Map.FromFile()!");

            int PreviousLevel = Reader.ReadInt32();
            int NextLevel = Reader.ReadInt32();

            string StrPreviousLevel = PreviousLevel.ToString() + ".fld";
            string StrNextLevel = NextLevel.ToString() + ".fld";

            int Width = Reader.ReadInt32();
            int Height = Reader.ReadInt32();
            MessageBox.Show("Width: " + Width.ToString() + " Height: " + Height.ToString());

            int[,] Grid = new int[Width, Height]; 

            for (int x = 0; x < Width; x++)
            {
                for (int y = 0; y < Height; y++)
                {
                    int Tile = Reader.ReadInt32();

                    if (Tile > 0)
                        Grid[x, y] = Tile / 2;
                    else
                        Grid[x, y] = Tile;
                }
            }

            Reader.Close();

            return new Map(Width, Height, Grid, StrNextLevel);
        }

I've checked the file that's written by my level editor in Hex Workshop, and the values of Width and Height are -supposed- to be 64 and 64. But they're not being read as such!! Here's the writing function:
        /// <summary>
        /// Saves the currently generated map to a FLD level file.
        /// </summary>
        /// <param name="Path">The path of the file to write tp, including name.
        /// Will be created if it doesn't already exist.</param>
        public void ToFile(string Path, string PreviousLvl, string NextLvl)
        {
            BinaryWriter Writer = new BinaryWriter(File.Create(Path));
            ASCIIEncoding Encoding = new ASCIIEncoding();

            //Signature - Flexible Level Data
            Writer.Write(Encoding.GetBytes("FLD"));
            //Version
            Writer.Write((int)1);
            //Previous Level
            Writer.Write((int)int.Parse(PreviousLvl));
            //Next Level
            Writer.Write((int)int.Parse(NextLvl));
            //Level Width
            Writer.Write((int)mapWidth);
            //Level Height
            Writer.Write((int)mapHeight);

            for (int x = 0; x < mapWidth; x++)
            {
                for (int y = 0; y < mapHeight; y++)
                {
                    Writer.Write((int)(grid[x, y ] * 2));
                }
            }

            Writer.Close();
        }

What's wrong here? There must be something I'm not seeing. Am I blind? o_O Edit: If anyone's wondering, the error I'm getting in the reading function is, obviously, 'Cannot read beyond the end of the stream'.
Advertisement
Just a couple of thoughts: the other values are read correctly? Have you tried to set the reader to the start of the stream explicitly? It shouldn'matter at all, but are you on a 32 or 64 bit system?
I'm running Windows XP 32 bit.
The other values are read correctly, so I didn't see the point in setting the reader to the beginning of the stream. Suppose I could always try it though!
Setting the Reader to the beginning of the stream didn't work, either. :\
The code works fine for me.
A couple of thoughts:

Are you sure you read the SAME file back in?
Are you sure you are WRITING the correct values for width and height? (one method is static and the other is an instance method)
The only thing I'm kinda... in doubt about is that for the writing method, mapWidth and mapHeight are const in the class of the method. I think I should try making them not const.

Edit: D'oh! Turns out I had forgotten to copy the newly generated files (the ones I were sure were working) from the map editor into the 'bin' folder of the game project.
Now it works perfectly!
Hey man if you set you files to "Copy To Local" they will go with your build automatically by VS

This topic is closed to new replies.

Advertisement