2012-11-03 17 views
0

我试图本地高分系统添加到我的比赛,有一个整型砖墙与文件读写运行,根据程序员和讲师我知道在现实生活中和在互联网上的一些教程我的代码应该工作,因为我想要它,但是无论何时我开始我的游戏我的高分不加载,下面是我的阅读和写入文件的功能,是有我犯的错误吗?我的WP7游戏要么不读或从IsolatedFileStream书写正确

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 
     } 
    } 
} 

回答

3

首先,读取部分存在错误:您打开的文件为FileMode.Create

而且根据documentation

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

因此,基本上,您的ReadHighScore正在删除旧文件并创建一个新文件。我相信,这不是你想要做的。用FileMode.OpenOrCreate代替FileMode.Create(只在ReadHighScore方法中),你应该有一个更好的结果。


此外,还有在SaveHighScore错误,在这条线:

stream.Write(myByteArray, 0, streamLength); 

既然你创建的文件,streamLength应该是等于0。因此,你是不是写任何东西。你真正想要做的是:

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

您应该考虑使用StreamReaderStreamWriterBinaryReader,并BinaryWriter读取/写入流,因为它们更容易使用。


最后但并非最不重要的是,您正在阅读和写作的数据是错误的。在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); 
      } 
     } 
    } 
} 
+0

哇哦,谢谢!而且你已经告诉我,即使我能做的事情总是可以做得更干净,我仍然有很多要学习:) – TotalJargon

+0

代码仍然需要一点工作,错过了一个EndOfStreamException,所以我想我'现在必须学会如何去做:P感谢您的帮助! – TotalJargon

+0

@TotalJargon的确我错过了那一个。第一次调用'ReadHighScore'时,该文件将为'BinaryReader'为空将抛出异常。你可以事先检查一下流的长度,或者在打开它之前检查文件是否存在(如果它不存在,退出函数) –