How to sort the text file
Posted on May 1, 2009
Here is the vb6 code to sort the text file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | Option Explicit Option Compare Text Public Sub SortTextFile(pstrFile As String) Dim strLine() As String strLine = Split(LoadFileToString(pstrFile), vbNewLine) QuickSort1 strLine SaveStringToFile Join(strLine, vbNewLine), pstrFile End Sub Public Function LoadFileToString(pstrFile As String) As String Dim bytArray() As Byte Dim strReturn as String Dim FileNumber As Long FileNumber = FreeFile Open pstrFile For Binary Access Read As #FileNumber ReDim bytArray(LOF(FileNumber) - 1) Get #FileNumber, 1, bytArray Close #FileNumber strReturn = StrConv(bytArray, vbUnicode) Erase bytArray Do While Instr(strReturn, vbNewLine & vbNewline) strReturn = Replace(strReturn, vbNewLine & vbNewline, vbNewLine) Loop Do While Right(strReturn, 2) = vbNewLine strReturn = Left(strReturn, Len(strReturn) - 2) Loop LoadFileToString = strReturn End Function Public Sub SaveStringToFile(pstrText As String, pstrFile As String) Dim FileNumber As Long If Len(Dir(pstrFile)) Then Kill pstrFile FileNumber = FreeFile() Open pstrFile For Output As #FileNumber Print #FileNumber, pstrText Close End Sub ' Omit plngLeft & plngRight; they are used internally during recursion Public Sub QuickSort1(ByRef pvarArray As Variant, Optional ByVal plngLeft As Long, Optional ByVal plngRight As Long) Dim lngFirst As Long Dim lngLast As Long Dim varMid As Variant Dim varSwap As Variant If plngRight = 0 Then plngLeft = LBound(pvarArray) plngRight = UBound(pvarArray) End If lngFirst = plngLeft lngLast = plngRight varMid = pvarArray((plngLeft + plngRight) \ 2) Do Do While pvarArray(lngFirst) < varMid And lngFirst < plngRight lngFirst = lngFirst + 1 Loop Do While varMid < pvarArray(lngLast) And lngLast > plngLeft lngLast = lngLast - 1 Loop If lngFirst <= lngLast Then varSwap = pvarArray(lngFirst) pvarArray(lngFirst) = pvarArray(lngLast) pvarArray(lngLast) = varSwap lngFirst = lngFirst + 1 lngLast = lngLast - 1 End If Loop Until lngFirst > lngLast If plngLeft < lngLast Then QuickSort1 pvarArray, plngLeft, lngLast If lngFirst < plngRight Then QuickSort1 pvarArray, lngFirst, plngRight End Sub 'How to use this routine: 'call SortTextFile ("C:\Temp\testFile.txt") |
May 29th, 2009 - 02:59
Thanks for writing, I really liked your most recent post. I think you should post more frequently, you evidently have natural ability for blogging!