• 12
• 12
• 9
• 10
• 13

# [.net] Reading values stored in a file?

This topic is 3141 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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)
{
ASCIIEncoding Encoding = new ASCIIEncoding();

if (Signature != "FLD")

if (Version != 1)

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

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++)
{

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

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 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 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 on other sites
Setting the Reader to the beginning of the stream didn't work, either. :\

##### 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 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!