- Home /
Question by
Noaal · May 30, 2014 at 07:56 AM ·
highscoressystem.iotxt
Highscores in text file
Hi, I want to store highscores in external txt file and when game is over read them from this file. My file is something like that: "PlayerName,score". Here is my code:`int PlayerScore; FileStream fso = new FileStream (@"Highscores.txt", FileMode.Append, FileAccess.Write, FileShare.Read); FileStream fsr = new FileStream (@"Highscores.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
public void WriteHighScore(string name, int score)
{
using (StreamWriter sw = new StreamWriter(fso))
{
sw.WriteLine(name +","+ score);
sw.Close();
}
}
public string ReadHighScore(string name,int newHscore)
{
using (StreamReader sr = new StreamReader(fsr))
{
string line;
while ((line = sr.ReadLine()) != null)
{
string [] words = line.Split(',');
foreach (string word in words)
{
int Hscore = Int32.Parse(words[1]);
if(newHscore > Hscore)
{
WriteHighScore("Drik",newHscore);
}
}
return line;
}
}
return null;
}
When I'm at the gameover screen I have 2 errors:
System.IO.StreamWriter..ctor (System.IO.Stream stream, System.Text.Encoding encoding, Int32 bufferSize) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.IO/StreamWriter.cs:95) System.IO.StreamWriter..ctor (System.IO.Stream stream) (wrapper remoting-invoke-with-check) System.IO.StreamWriter:.ctor (System.IO.Stream) HighScoreManager.WriteHighScore (System.String name, Int32 score) (at Assets/Scripts/HighScoreManager.cs:23) HighScoreManager.ReadHighScore (System.String name, Int32 newHscore) (at Assets/Scripts/HighScoreManager.cs:44) GameOver.OnGUI () (at Assets/Scripts/GameOver.cs:33)ArgumentException: Can not write to stream
System.IO.StreamReader.Initialize (System.IO.Stream stream, System.Text.Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.IO/StreamReader.cs:178) System.IO.StreamReader..ctor (System.IO.Stream stream, System.Text.Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.IO/StreamReader.cs:139) System.IO.StreamReader..ctor (System.IO.Stream stream) (wrapper remoting-invoke-with-check) System.IO.StreamReader:.ctor (System.IO.Stream) HighScoreManager.ReadHighScore (System.String name, Int32 newHscore) (at Assets/Scripts/HighScoreManager.cs:32) GameOver.OnGUI () (at Assets/Scripts/GameOver.cs:33) But when I chceck my file the new lines are appearing, but they can't be read and presented on the screen. What is problem here?ArgumentException: Cannot read stream
Comment