Click to See Complete Forum and Search --> : Compare two text files line by line
asf14
May 1st, 2010, 05:00 PM
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.
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!!
dglienna
May 1st, 2010, 05:21 PM
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.
DataMiser
May 1st, 2010, 07:35 PM
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.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.
asf14
May 1st, 2010, 10:32 PM
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!
asf14
May 3rd, 2010, 09:31 PM
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!!
dglienna
May 3rd, 2010, 10:12 PM
1) Integer
2) That's a VB6 shortcut for vbCrLf, Carriage-Return & Linefeed
DataMiser
May 3rd, 2010, 10:13 PM
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.
DataMiser
May 3rd, 2010, 10:14 PM
Looks like you beat me to the post :)
dglienna
May 3rd, 2010, 10:18 PM
First one, tonight
asf14
May 4th, 2010, 02:36 PM
Thanks again for your help! I got it to work! If anybody is trying to find the right code here it is:
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
DataMiser
May 4th, 2010, 03:10 PM
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.
asf14
May 5th, 2010, 01:44 AM
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.
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
asf14
May 5th, 2010, 08:49 AM
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?
asf14
May 7th, 2010, 10:29 AM
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
DataMiser
May 7th, 2010, 11:53 AM
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.
asf14
May 7th, 2010, 12:29 PM
Hey DataMiser, well the problem is that it actually adds the elasped time which is in textbox 3 and for some odd reason it adds the second's time in textbox 2 even though it clearly only saves the text in textbox3. This is why it would read:
3, 45, 5, 47, 49, 7
But sometimes it doesn't read all the second's time in textbox2 and the order is off because the 7 would be before the 49. I'm not sure why this would even happen! :(
asf14
May 7th, 2010, 12:32 PM
This is the actual text file created by the user's steps:
2 : 247
6 : 247
36 : 247
6 : 247
36 : 247
7 : 247
37 : 247
8 : 251
38 : 251
8 : 251
38 : 251
9 : 251
39 : 251
10 : 243
10 : 243
40 : 247
10 : 247
40 : 247
11 : 254
41 : 254
11 : 247
41 : 247
12 : 247
42 : 247
12 : 251
42 : 251
17 : 254
47 : 254
48 : 251
19 : 251
21 : 247
51 : 247
21 : 247
51 : 247
DataMiser
May 7th, 2010, 03:02 PM
I think you need to post the code, I can not tell what may be going on from the portions you have posted but somewhere either there is a line that is assigning data to the text box that you do not want or writing something to the file that should not be written, To find these things the correct procedure is to step through each line of code in debug mode and notice the contents of any related variables.
Somewhere you are telling it to write what you are getting just have to find it. If you post the code I'll take a quick look and see if anything jumps out at me.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.