Console Application - Noughts And Crosses

ytotheb

New member
Joined
Mar 6, 2010
Messages
2
Programming Experience
1-3
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!
 
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 do you reach this conclusion? Your SaveGame method creates a new file each time it is called, it is not appending.
 
This is the error message it comes up with when i try to save again after already having saved...

The process cannot access the file 'F:\NoughtsAndCrosses.Txt' because it is being used by another process.
 
Your code calls Close so the problem must be elsewhere.
 
How do you reach this conclusion? Your SaveGame method creates a new file each time it is called, it is not appending.

I don't see his code creating a new file at all, aside from the first time it is run. He is creating a new StreamWriter, but that would not clear the file when it is instantiated. Personally, I would use a FileStream instead of a StreamWriter, as you could set its FileMode to Create, which will create the file if it does not exist, or clear the file if it does exist.

Your code calls Close so the problem must be elsewhere.

I'm not exactly sure what the issue is here. The only issue that I can think of right now is that your DisplayScore is starting an infinite recursion and is not allowing the code to continue to close the file reader. If you post your DisplayScore code, I can look at it and see if that is happening.
 
I don't see his code creating a new file at all, aside from the first time it is run. He is creating a new StreamWriter, but that would not clear the file when it is instantiated. Personally, I would use a FileStream instead of a StreamWriter, as you could set its FileMode to Create, which will create the file if it does not exist, or clear the file if it does exist.
That is the default mode of StreamWriter (which uses a FileStream), it just saves you creating the stream then wrapping the writer, ie a shortcut. To have StreamWriter append you must explicitly set that flag.
 
Hmm, Your streamreder.close comes after you call a sub routine. In that subroutine is that particular path used again ? if so, therein lies your problem :)
 
Back
Top