- #1
phion
Gold Member
- 175
- 39
Hi,
I'm having trouble figuring out how to copy the contents of a file over to another file, and removing an item of the content of the list I'm copying in the process. The idea is that I need to remove an item from a list by copying everything that is in file one over to file two that is not the item I need to remove, deleting the original file, and the renaming the second file to the original. I can't seem to get it to work.
Here's the function that main calls.
I'm having trouble figuring out how to copy the contents of a file over to another file, and removing an item of the content of the list I'm copying in the process. The idea is that I need to remove an item from a list by copying everything that is in file one over to file two that is not the item I need to remove, deleting the original file, and the renaming the second file to the original. I can't seem to get it to work.
Here's the function that main calls.
Code:
Function removePlayer(playerList() As String, numPlayers As Integer) As Integer
Dim updatedPlayers As Integer
Dim player As String
Dim fileName As String = "playerList.txt"
Dim tempFileName As String = "tempPlayerList.txt"
Dim inFile As New StreamReader(fileName)
Dim outFile As New StreamWriter(tempFileName)
Console.WriteLine("Which player would you like to remove?")
player = Console.ReadLine()
While Not inFile.EndOfStream
For i = 0 To numPlayers - 1
playerList(i) = inFile.ReadLine()
If playerList(i) <> player Then
outFile.WriteLine(playerList(i))
End If
Next
End While
inFile.Close()
outFile.Close()
File.Delete("playerList.txt")
My.Computer.FileSystem.RenameFile("C:\Users\John\Desktop\addPLayerList\bin\Debug\tempPlayerList.txt", "playerList.txt")
File.Create("C:\Users\John\Desktop\addPLayerList\bin\Debug\tempPlayerList.txt")
updatedPlayers = numPlayers - 1
Return updatedPlayers
End Function