Hi,
I'm doing an exam in june and we've been provided with a skeleton program that we need to change, making it work and adding additional features to the program.
I've added a savegame sub which saves the score of the game, and also a load game which access the saved file and loads the scores.
Sub SaveGame()
Dim FileName As String
FileName = "F:\NoughtsAndCrosses.Txt"
CurrentFileWriter = New StreamWriter(FileName)
'To make loading the scores back into the program easier
CurrentFileWriter.WriteLine(PlayerOneName)
CurrentFileWriter.WriteLine(PlayerOneScore)
CurrentFileWriter.WriteLine(PlayerTwoName)
CurrentFileWriter.WriteLine(PlayerTwoScore)
CurrentFileWriter.Close()
Console.WriteLine("Your Score Has Been Successfully Saved")
Main()
End Sub
Sub LoadGame()
'Now we know there will be exactly 4 lines
Dim FileName As String
FileName = "F:\NoughtsAndCrosses.Txt"
CurrentFileReader = New StreamReader(FileName)
'Checking that the file isnt empty
If Not CurrentFileReader.EndOfStream Then
PlayerOneName = CurrentFileReader.ReadLine()
PlayerOneScore = CInt(CurrentFileReader.ReadLine())
PlayerTwoName = CurrentFileReader.ReadLine()
PlayerTwoScore = CInt(CurrentFileReader.ReadLine())
DisplayScore()
End If
CurrentFileReader.Close()
Main()
End Sub
The problem is, this method will fail if i try to save twice as the file will contain 8 lines and it will only read the first 4 lines..
How would i go about how to clearing the contents of the file before i write the four new lines in SaveGame() ?..
Thank you in advance!
I'm doing an exam in june and we've been provided with a skeleton program that we need to change, making it work and adding additional features to the program.
I've added a savegame sub which saves the score of the game, and also a load game which access the saved file and loads the scores.
Sub SaveGame()
Dim FileName As String
FileName = "F:\NoughtsAndCrosses.Txt"
CurrentFileWriter = New StreamWriter(FileName)
'To make loading the scores back into the program easier
CurrentFileWriter.WriteLine(PlayerOneName)
CurrentFileWriter.WriteLine(PlayerOneScore)
CurrentFileWriter.WriteLine(PlayerTwoName)
CurrentFileWriter.WriteLine(PlayerTwoScore)
CurrentFileWriter.Close()
Console.WriteLine("Your Score Has Been Successfully Saved")
Main()
End Sub
Sub LoadGame()
'Now we know there will be exactly 4 lines
Dim FileName As String
FileName = "F:\NoughtsAndCrosses.Txt"
CurrentFileReader = New StreamReader(FileName)
'Checking that the file isnt empty
If Not CurrentFileReader.EndOfStream Then
PlayerOneName = CurrentFileReader.ReadLine()
PlayerOneScore = CInt(CurrentFileReader.ReadLine())
PlayerTwoName = CurrentFileReader.ReadLine()
PlayerTwoScore = CInt(CurrentFileReader.ReadLine())
DisplayScore()
End If
CurrentFileReader.Close()
Main()
End Sub
The problem is, this method will fail if i try to save twice as the file will contain 8 lines and it will only read the first 4 lines..
How would i go about how to clearing the contents of the file before i write the four new lines in SaveGame() ?..
Thank you in advance!