Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compare 2 text files using vb6????? 1

Status
Not open for further replies.

likelylad

IS-IT--Management
Joined
Jul 4, 2002
Messages
388
Location
GB
I have 2 text files.
One text file will be on the local drive and will contain predefined characters.

The other text file will be on a floppy disk (that may contain a number of text files)

What I would like to do is open the files on the floppy disk and compare each character against the file on the local drive.

The program should then say how many of the predefined characters are in each file on the floppy disk.

Thanking you in advance for any help received
 
How about:

-------------------------------------
Dim InFile As Byte, i As Long, j As Long
Dim n1 As Long, n2 As Long, n As Long
Dim f1() As Byte, f2() As Byte

InFile = FreeFile
Open "c:\tmp\dt.txt" For Binary Access Read As #InFile
n1 = LOF(InFile)
ReDim f1(n1)
Get #InFile, , f1
Close #InFile
InFile = FreeFile
Open "c:\tmp\dt2.txt" For Binary Access Read As #InFile
n2 = LOF(InFile)
ReDim f2(n2)
Get #InFile, , f2
Close #InFile

n = IIf(n1 < n2, n1, n2)
j = 0
For i = 0 To n Step 2
If f1(i) <> f2(i) Then j = j + 1
Next i

MsgBox CStr(j + Abs(n1 - n2)) & &quot; different characters&quot;
------------------------------------------------- Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Hi Sunaj

I am getting a compile error
&quot;Invalid Outside Procedure&quot;

Also is there a way I can browse to file???

 
You need to put the code into a sub. E.g. the click event of a command button.
Use the common dialog contol to browse for a file.
Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Hi sunaj

I orginally had put it into the click event of a command button.

Are there any references I need to make sure are available???
 
Hi sunaj

I see what was wrong!!!!!
 
Hi Sunaj

I put

h
e
into one of the files

hellohellohellohellohellohellohellohellohellohellohellohellohellohellohello

in the second file

The expected answer was 30
The answer given is 72
 
hello hello
If you put in an extra character, the rest of the file will come out as different (because you'll compare 'shifted').
Step through the code in debug mode to see what happens.

Good luck Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
AS ALWAYS, the following is NOT intended as production code. In this instance, it is not even intended to respond to all of the original posters question, but just to correct some simple text array handlling within the routine.


Code:
Public Function basCntOccur(MyFil1 As String, MyFil2 As String) As Long

    Dim InFile As Byte
    Dim Idx As Long
    Dim Jdx As Long
    Dim LenFil1 As Long
    Dim LenFil2 As Long
    Dim LenFil As Long
    Dim Fil1 As String
    Dim Fil2 As String
    Dim MyAry1 As Variant
    Dim MyAry2 As Variant

    '? basCntOccur(&quot;c:\temp\dt.txt&quot;, &quot;c:\temp\dt2.txt&quot;)
    '30

    'h
    'E
    'into one of the files (&quot;c:\temp\dt.txt&quot;)

    'hellohellohellohellohellohellohellohellohellohellohellohellohellohellohello
    'in the second file (&quot;c:\temp\dt2.txt&quot;)

    'The expected answer was 30
    'The answer given is 72
 
    InFile = FreeFile
    Open MyFil1 For Binary Access Read As #InFile
    LenFil1 = LOF(InFile)
    Fil1 = String(LOF(InFile), Space(1))
    Get #InFile, , Fil1
    Close #InFile

    InFile = FreeFile
    Open MyFil2 For Binary Access Read As #InFile
    LenFil2 = LOF(InFile)
    Fil2 = String(LOF(InFile), Space(1))
    Get #InFile, , Fil2
    Close #InFile

    LenFil = basMinVal(LenFil1, LenFil2)
    
    MyAry1 = Split(Fil1, vbCrLf)
    While Idx <= UBound(MyAry1)
        MyAry2 = Split(Fil2, MyAry1(Idx))
        Jdx = Jdx + UBound(MyAry2)
        Idx = Idx + 1
    Wend

    basCntOccur = Jdx

End Function
[code]


[b]Sunaj[/b]

Why are regressing?  I know you implemented this reading a text file into a string PROPERLY just within the last week or so.  Where did the byte array part come from?  Both of the files are represented as simple (ASCII) text, so VB will dynamically reference them 'properly' as simple strings without the complication you introduce

 MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
MichaelRed
Regressing..... hmmm

The code I showed above is extremely fast. Split (which I know you like ;-)) is very slow. Try running my code on say two 15 Mb files -which on my 500 mHz pc is done in a couple of seconds. You cannot achive that kind of speed in you use string manipulating functions (e.g. mid, split).


Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Hi MichaelRed

With a small bit of changing I got your code to do exactly what I wanted.

Also Sunaj, thanks for your time spent on this
 
O,K. What 'speed' is achieved w/ the &quot;split&quot; version?

P.S. I do not have (and Do Wnat of NEED even ONE 500MB file, so am not in a position to really test it.

My impression is this is ONLY (by likelylad) for files which are much smaller than 15MB, but that -like the lack of error trapping- is left to the 'eye of the beholder'.

I am aware of the 'speed' of split, and -YES- I do like it despite the limitation. I &quot;Like It&quot; so much that I wrote a version of it for Ms. A. '97 users. In MOST instances, I prefer simplicity and flexibility to speed. Use of Split and Join cover a LOT of the string manipulation functions from earlier versions -with a reduction in the programming requirements of maintaining indicies for string positions and lengths.

Using much the same technique presented here, I have had other users report that there was essientially no doticable delay in parseing ~ 50MB of text into an array, Again, I realize the user reports do not note the system / machine characteristics or provide actual 'timing', but they do suggest that the differences are not huge.

MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top