Compare two text files line by line
Hello everyone!
I've been trying to compare two text files line by line but I was only able to compare the file lengths. I'm creating a dance mat game and I have a text file of the steps that should be taken to get a perfect score and a text file of the actual steps taken by the user. Each text file contains a time stamp and a binary value indicating which arrow was stepped on. I'm using streamreader and streamwriter to write and read the text files.
Code:
Private Sub Score_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim GameScore As String = "C:\SavedGame.txt"
If System.IO.File.Exists(GameScore) = True Then
Dim objReader As New System.IO.StreamReader(GameScore)
ValueBox.Text = objReader.ReadToEnd
objReader.Close()
End If
Dim intLines As Integer = ValueBox.Lines.Length
ScoreBox.Text = intLines
Dim Survive As String = "C:\I_Will_Survive.txt"
Dim intLinesSurvive As Integer = PointsPossibleBox.Lines.Length
PercentageBox.Text = (intLines / intLinesSurvive) * 100
Dim f1 As New IO.StreamReader(GameScore)
Dim f2 As New IO.StreamReader(Survive)
CorrectSteps.Text = f2.ReadToEnd
Dim LineNum As Integer
Do While f1.Peek <> -1 And f2.Peek <> -1 'loop while there is stuff in the files
LineNum += 1
Dim f1Line As String = f1.ReadLine
Dim f2line As String = f2.ReadLine
If LineNum = Survive Then
If f1Line = f2line Then
PercentageBox.Text = (f1Line / f2line)
End If
End If
Loop
End Sub
Please let me know if how I can modify my code to make it compare the two text files line by line! Thanks!!
Re: Compare two text files line by line
What do you want to do with the info? Count the differences, or SEE if there are differences?
If they should be the same, you can use one statement to set a flag.
Re: Compare two text files line by line
Personally I would read the entire files into a string and then split that into an array and loop through the arrays.
Something like this.
Code:
Dim File1 as New IO.StreamReader("\File1.Dat")
Dim FileData as string=File1.ReadToEnd()
File1.Close
Dim File1Data as String() =FileData.Split(vbCR)
Dim File2 as New IO.StreamReader("\File2.Dat")
FileData=File2.ReadToEnd()
File2.Close
Dim File2Data as String() =FileData.Split(vbCR)
For x=0 to Ubound(File1Data)
if File1Data(x)<>File2Data(x) then
'Files do not match
Else
'This Line matches
End If
Next
note: the code above was just typed here in this editor so may contain errors but should give you an idea.
Re: Compare two text files line by line
Thanks for the responses! dglienna, I want to count the number of lines that match and the number of lines that do not match and place those two values in a textbox.
Thanks DataMiser for the quick response, I will try reading the entire file into a string and splitting them up into arrays!
Re: Compare two text files line by line
Hey DataMiser, thanks for the sample code! I was wondering what the x would be in this case? What should it be declared as? Also what does the vbCR stand for? Thanks again for your time and help!!
Re: Compare two text files line by line
1) Integer
2) That's a VB6 shortcut for vbCrLf, Carriage-Return & Linefeed
Re: Compare two text files line by line
X would be an integer unless your file has over 64,000 lines in it.
vbCR is the constant for a Carriage return [can also be represented as CHR(13) ] basically this will cause the file to be broken down at each CR resulting in one line per element in the array. There will be a carry over LineFeed at the start of each line after the first but since it will be in both files should not pose a problem when doing the compare.
Re: Compare two text files line by line
Looks like you beat me to the post :)
Re: Compare two text files line by line
Re: Compare two text files line by line
Thanks again for your help! I got it to work! If anybody is trying to find the right code here it is:
Code:
Dim Party As String = "C:\Party.txt" 'List made of the correct steps to compare
Dim PartyText As New System.IO.StreamReader(Party) 'Reads the text file
CorrectSteps.Text = PartyText.ReadToEnd 'displays i will survive text file in rich textbox
Dim intLinesParty As Integer = CorrectSteps.Lines.Length ' counts number of lines in the text box
PointsPossibleBox.Text = intLinesParty
Dim intLinesCorrect As Integer 'number of lines that match
Dim intLinesWrong As Integer ' number of lines that do not match
Dim f1 As New IO.StreamReader(GameScore)
Dim fileData As String = f1.ReadToEnd()
f1.Close()
Dim file1Data As String() = fileData.Split(vbCr)
Dim f2 As New IO.StreamReader(Party)
fileData = f2.ReadToEnd()
f2.Close()
Dim file2Data As String() = fileData.Split(vbCr)
Dim x As Integer
For x = 0 To UBound(file1Data)
If file1Data(x) <> file2Data(x) Then
intLinesWrong = intLinesWrong + 1
Else
intLinesCorrect = intLinesCorrect + 1
End If
Next
WrongSteps.Text = (intLinesWrong - 1)
ScoreBox.Text = intLinesCorrect - 1
PercentageBox.Text = (intLinesCorrect / intLinesParty) * 100
Re: Compare two text files line by line
One thing of note here is you are not closing the PartyText file once you read it.
You should close it as soon as it has been read into the system.
Notice the way I coded the file routines to open, read, close the files.
Re: Compare two text files line by line
Hey, for some strange reasons this code doesn't work anymore...
When a correct step is taken and the text file displays:
9 : 247
and the text file comparing it to has the same line, it doesn't add 1.
Code:
Private Sub Score_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim GameScore As String = "C:\SavedGame.txt"
If System.IO.File.Exists(GameScore) = True Then
Dim objReader As New System.IO.StreamReader(GameScore)
ValueBox.Text = objReader.ReadToEnd 'displays SavedGame.txt in the rich text box
objReader.Close()
End If
'Dim intLines As Integer = ValueBox.Lines.Length
Dim Survive As String = "C:\I_Will_Survive.txt" 'List made of the correct steps to compare
Dim SurviveText As New System.IO.StreamReader(Survive) 'Reads the text file
CorrectSteps.Text = SurviveText.ReadToEnd 'displays i will survive text file in rich textbox
SurviveText.Close()
Dim intLinesSurvive As Integer = CorrectSteps.Lines.Length ' counts number of lines in the text box
PointsPossibleBox.Text = intLinesSurvive
Dim intLinesCorrect As Integer 'number of lines that match
Dim intLinesWrong As Integer ' number of lines that do not match
Dim f1 As New IO.StreamReader(GameScore)
Dim fileData As String = f1.ReadToEnd()
f1.Close()
Dim file1Data As String() = fileData.Split(vbCr)
Dim f2 As New IO.StreamReader(Survive)
fileData = f2.ReadToEnd()
f2.Close()
Dim file2Data As String() = fileData.Split(vbCr)
Dim x As Integer
For x = 0 To UBound(file1Data)
If file1Data(x) <> file2Data(x) Then
intLinesWrong = intLinesWrong + 1
Else
intLinesCorrect = intLinesCorrect + 1
End If
Next
WrongSteps.Text = (intLinesWrong - 1)
ScoreBox.Text = intLinesCorrect
PercentageBox.Text = (intLinesCorrect / intLinesSurvive) * 100
End Sub
Re: Compare two text files line by line
Hey, I'm still having issues with the files not displaying the number of correct steps. Anybody have any idea why the code above does not work?
Re: Compare two text files line by line
Hello,
This there a way to make the text files read in numerical order (1-100)? The values display randomly and I believe the reason is that the lines only match when they are at the same line count.
For example if I have one text file with
1, 2, 3
and the second game file that has:
4, 1, 5
It would display that none of them match but then its on the same line count it says it matches. This is possible to have the whole file read the text files and find identical lines even if they are not on the same line count?
Thanks
Re: Compare two text files line by line
If the correct sequence was 1,2,3 and they did 2,3,1 that would be wrong even though the the numbers are present in both files. If you sorted numerically and then compared it would think that 2,3,1 was perfect even though everything would be incorrect.
As for how you could do it, one way would be to add a couple of listboxes and set the sorted property to true then add the values from each file into the lists and then compare the items in the lists, not sure why you would want to do such a thing however as if you are trying to score a dance routine they need to make the correct steps in the correct order any sorting would make the data invalid.