0

根据我在现实生活中认识的程序员和讲师以及互联网上的一些教程,我的代码应该可以正常工作正如我想要的那样,但是每当我开始我的游戏时,我的高分都没有加载,下面是我读取和写入文件的函数,我犯了什么愚蠢的错误吗?

public void ReadHighScore()
    {
        byte[] myByteArray = new byte[64]; // Creates a new local byte array with a length of 64
        using (var store = IsolatedStorageFile.GetUserStoreForApplication()) // Creates an IsolatedStorageFile within the User Storage
        using (var stream = new IsolatedStorageFileStream("highscore.txt", System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite, store)) // Creates a new filestream attatched to the storage file
        {
            if (stream != null) // Checks to see if the filestream sucessfully read the file
            {
                int streamLength = (int)stream.Length; // Gets the length of the filestream
                stream.Read(myByteArray, 0, streamLength); // Parses the filestream to the byte array
            }
            else
                myState = (int)game_state.Terminate; // Temporary Error checking, the function gets though this without triggering the 'terminate' gamestate
        }

        string ScoreString = myByteArray.ToString(); // Parses the byte array to a string
        Int32.TryParse(ScoreString, out highScore.score); // Parses the string to an integer
    }

    public void SaveHighScore()
    {
        byte[] myByteArray = new byte[64]; // Creates a new local byte array with a length of 64
        using (var store = IsolatedStorageFile.GetUserStoreForApplication()) // Creates an IsolatedStorageFile within the User Storage
        using (var stream = new IsolatedStorageFileStream("highscore.txt", System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite, store)) // Creates a new filestream attatched to the storage file
        {
            if (stream != null) // Checks to see if the filestream sucessfully read the file
            {
                int streamLength = (int)stream.Length; // Gets the length of the filestream
                stream.Write(myByteArray, 0, streamLength); // Parses the byte array to the filestream
            }
            else
                myState = (int)game_state.Terminate; // Temporary Error checking, the function gets though this without triggering the 'terminate' gamestate
        }
    }
}
4

1 回答 1

3

首先,阅读部分有一个错误:您正在使用FileMode.Create.

并根据文档

指定操作系统应该创建一个新文件。如果文件已经存在,它将被覆盖。

所以基本上,你ReadHighScore正在删除旧文件并创建一个新文件。我相信,这不是你想做的。替换FileMode.CreateFileMode.OpenOrCreate(仅在ReadHighScore方法中),您应该有更好的结果。


此外,在这一行的 , 中有一个错误SaveHighScore

stream.Write(myByteArray, 0, streamLength);

由于您正在创建文件,streamLength因此应该等于 0。因此,您没有写任何东西。你真正想做的是:

stream.Write(myByteArray, 0, myByteArray.Length);

您应该考虑使用StreamReaderStreamWriterBinaryReaderBinaryWriter来读/写流,因为它们更容易使用。


最后但并非最不重要的一点是,您正在读取和写入的数据是错误的。在该SaveHighScore方法中,您试图保存一个空数组,实际的高分无处可寻。在该ReadHighScore方法中,情况更糟:您正在阅读myByteArray.ToString(),这将始终等于System.Byte[]


最后,你的代码应该是这样的:(假设highScore.score被声明为int

public void ReadHighScore()
{
    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (var stream = new IsolatedStorageFileStream("highscore.txt", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Read, store))
        {
            using (var reader = new BinaryReader(stream))
            {
                highScore.score = reader.ReadInt32();
            }
        }
    }
}

public void SaveHighScore()
{
    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (var stream = new IsolatedStorageFileStream("highscore.txt", System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite, store))
        {
            using (var writer = new BinaryWriter(stream))
            {
                writer.Write(highScore.score);
            }
        }
    }
}
于 2012-11-03T14:07:56.077 回答