Sign in to follow this  
MatsVed

[.net] Reading values stored in a file?

Recommended Posts

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'.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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!

Share this post


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

Sign in to follow this