text file operations with vb6.0

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pongscript
    New Member
    • Mar 2007
    • 27

    #31
    but if you have a device that log it to a text file
    database has no use if you want to directly query the textfile

    database are only used for hierarchical data but these type operation
    doesnt require more than a plain text format.

    and besides why use temporary database if you could use an array...
    array as faster than temporary database like access..

    Comment

    • galahad
      New Member
      • Mar 2007
      • 2

      #32
      yep... you might be right about speed of processing... by how much an hour? but if you're developing and you have a deadline. do what is the fastest right? :D

      it's just a suggestion. sometimes we trade processing time, for faster output and flexibility. and what if you have a new requirement for you report? then what? :D redo the program again? that's what i'm saying... if the SCRIPT can do it... then it'll be faster in the long run... in terms of producing it.

      cheers.

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #33
        Originally posted by pongscript
        ...just email me @ Removed by Moderator if you find these suck or you want a breif explanation...
        Sorry, but I've erased your e-mail address. Please don't post e-mail addresses here, as forums like this are often scanned by spammers and scammers to pick up any e-mail addresses they can find.

        You have two real options...
        1. Just ask people to click on your ID, and from your profile they can hit the "send a message via email" link (if you have it enabled), or
        2. Obfuscate the address so that a person reading the post can see what you mean and translate it, but an automated scan is likely to miss it.
        TheScripts has a policy of discouraging the posting of e-mail addresses - see the FAQ.

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #34
          Originally posted by galahad
          yep... you might be right about speed of processing... by how much an hour? but if you're developing and you have a deadline. do what is the fastest right? :D
          Personally, I would consider the use of a database to be serious overkill in this case, and only to be used as a last resort. The task is actually quite a simple one, which can be accomplished by merely reading in the text file and writing out the result. Why add all the complexity and overheads of a database?

          I'm on lunch now, so I think I'll have a go at producing a fully working version. Stay tuned...

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #35
            Originally posted by Killer42
            Personally, I would consider the use of a database to be serious overkill in this case, and only to be used as a last resort. The task is actually quite a simple one, which can be accomplished by merely reading in the text file and writing out the result. Why add all the complexity and overheads of a database?

            I'm on lunch now, so I think I'll have a go at producing a fully working version. Stay tuned...
            Here you go, I believe this is a fully working version. At least it worked with a small data sample that I pulled from your earlier posts.

            A couple of things to keep in mind.
            • To keep things simple, I've commented out the entire section which validates your selection and builds the file name. I just assumed a file called "sample.swd " in the same directory as the project.
            • Results are written to "channelCount.t xt" in the same directory.
            • There's no error checking (of course).
            • This is written assuming that the homeid is the first 10 characters of each record - I seem to recall there was some question over whether it should be 10 or 12.
            • It might be more efficient to generate the count as you build the array. Each time a new entry is added to the array of unique HomeId/Channel combinations, you could increment a count for that HomeId (in another array). This would remove the need to run through the array later and count them.

            Code:
            Private Sub cmdclick_Click()
              
              Dim i As Long             ' General loop counter.
              Dim j As Integer          ' General loop counter.
              Dim FileName As String    ' File to be scanned.
              Dim SearchLine As String  ' Input line from data file.
              
              ' Used in building and sorting array of unique homeid/channel combinations...
              Dim Exists As Boolean
              Dim HomeAndChannel As String
              Dim ChannelArray(0 To 100000) As String
              Dim ArrayCount As Long                   ' Number of entries placed in the array.
              
              ' Used in counting the unique channels per homeid...
              Dim HomeId As String
              Dim PrevHomeId As String
              Dim ChannelCount As Integer
              
            
              ' prevcarray = ""
            '  For j = 0 To lstMarket.ListCount - 1
            '    If lstMarket.Selected(j) = True Then
            '      If Len(Trim(txtYear.Text)) > 2 Or IsNumeric(Trim(txtYear.Text)) = False Then
            '        MsgBox "This should be 2 digit number "
            '        txtYear.Text = ""
            '        txtYear.SetFocus
            '        Exit Sub
            '      End If
            '
            '      If Trim(txtWeek) <= 9 And Len(Trim(txtWeek)) < 2 Or (IsNumeric(txtWeek.Text) = False) Then
            '        txtWeek = "0" & Trim(txtWeek)
            '      End If
            '
            '      If Trim(txtDay) > 7 Or (IsNumeric(txtDay) = False) Then
            '        MsgBox "Please enter number which is equal to or less than 7"
            '        txtDay.Text = ""
            '        txtDay.SetFocus
            '        Exit Sub
            '      End If
            '
            '      filename = lstMarket.List(j) & Trim(txtYear) & Trim(txtWeek) & Trim(txtDay) & ".swd"
            '      MsgBox ("you have selected the following : " & filename)
            '    End If
            '  Next j
            
              FileName = App.Path & "\Sample.swd"
            
              ' ********main code for listing all the channels that the house id watch*******
                          
              If fsys.FileExists(FileName) Then
                MsgBox "file is open"
              Else
                MsgBox "file not found"
                Exit Sub
              End If
              
              Set txtstream = fsys.OpenTextFile(FileName, ForReading, False)
              Set outstream = fsys.OpenTextFile(App.Path & "\ChannelCount.txt", ForWriting, True)
              
              
              
              ' ****************************************
              ' Step 1: Build an array of all unique homeid/channel combinations
              ' ****************************************
              
              ArrayCount = 0
              Do Until txtstream.AtEndOfStream
                SearchLine = txtstream.ReadLine
                HomeAndChannel = Left(SearchLine, 10) & Mid(SearchLine, 13, 5)
                ' Check whether we have already seen this combination.
                Exists = False
                For i = 1 To ArrayCount
                  If ChannelArray(i) = HomeAndChannel Then
                    Exists = True
                    Exit For
                  End If
                Next
                ' If not, add it to the array.
                If Not Exists Then
                  ArrayCount = ArrayCount + 1
                  ChannelArray(ArrayCount) = HomeAndChannel
                End If
              Loop
              
              
              
              ' ****************************************
              ' Step 2: Sort the array.
              ' ****************************************
              
              Dim k As Long
              Dim tmpsort As String
              
              For i = 1 To ArrayCount - 1
                For j = ArrayCount - 1 To i Step -1
                  k = j + 1
                  If ChannelArray(j) > ChannelArray(k) Then
                    tmpsort = ChannelArray(j)
                    ChannelArray(j) = ChannelArray(k)
                    ChannelArray(k) = tmpsort
                  End If
                Next
              Next
              
              
              
              ' ****************************************
              ' Step 3: Scan the array and report the number of channels per homeid
              ' ****************************************
              
              ChannelCount = 1
              PrevHomeId = Left$(ChannelArray(1), 10)
              For i = 2 To ArrayCount
                HomeId = Left$(ChannelArray(i), 10)
                If HomeId = PrevHomeId Then
                  ChannelCount = ChannelCount + 1
                Else
                  outstream.WriteLine PrevHomeId & " " & Format(ChannelCount)
                  ChannelCount = 1
                  PrevHomeId = HomeId
                End If
              Next
              ' Write out the last entry, which will otherwise be missed.
              outstream.WriteLine PrevHomeId & " " & Format(ChannelCount)
              outstream.Close
              txtstream.Close
              
              MsgBox "process completed"
            
            End Sub

            Comment

            • Ronin
              New Member
              • Feb 2007
              • 25

              #36
              Originally posted by Killer42
              Here you go, I believe this is a fully working version. At least it worked with a small data sample that I pulled from your earlier posts.

              A couple of things to keep in mind.
              • To keep things simple, I've commented out the entire section which validates your selection and builds the file name. I just assumed a file called "sample.swd " in the same directory as the project.
              • Results are written to "channelCount.t xt" in the same directory.
              • There's no error checking (of course).
              • This is written assuming that the homeid is the first 10 characters of each record - I seem to recall there was some question over whether it should be 10 or 12.
              • It might be more efficient to generate the count as you build the array. Each time a new entry is added to the array of unique HomeId/Channel combinations, you could increment a count for that HomeId (in another array). This would remove the need to run through the array later and count them.

              Code:
              Private Sub cmdclick_Click()
                
                Dim i As Long             ' General loop counter.
                Dim j As Integer          ' General loop counter.
                Dim FileName As String    ' File to be scanned.
                Dim SearchLine As String  ' Input line from data file.
                
                ' Used in building and sorting array of unique homeid/channel combinations...
                Dim Exists As Boolean
                Dim HomeAndChannel As String
                Dim ChannelArray(0 To 100000) As String
                Dim ArrayCount As Long                   ' Number of entries placed in the array.
                
                ' Used in counting the unique channels per homeid...
                Dim HomeId As String
                Dim PrevHomeId As String
                Dim ChannelCount As Integer
                
              
                ' prevcarray = ""
              '  For j = 0 To lstMarket.ListCount - 1
              '    If lstMarket.Selected(j) = True Then
              '      If Len(Trim(txtYear.Text)) > 2 Or IsNumeric(Trim(txtYear.Text)) = False Then
              '        MsgBox "This should be 2 digit number "
              '        txtYear.Text = ""
              '        txtYear.SetFocus
              '        Exit Sub
              '      End If
              '
              '      If Trim(txtWeek) <= 9 And Len(Trim(txtWeek)) < 2 Or (IsNumeric(txtWeek.Text) = False) Then
              '        txtWeek = "0" & Trim(txtWeek)
              '      End If
              '
              '      If Trim(txtDay) > 7 Or (IsNumeric(txtDay) = False) Then
              '        MsgBox "Please enter number which is equal to or less than 7"
              '        txtDay.Text = ""
              '        txtDay.SetFocus
              '        Exit Sub
              '      End If
              '
              '      filename = lstMarket.List(j) & Trim(txtYear) & Trim(txtWeek) & Trim(txtDay) & ".swd"
              '      MsgBox ("you have selected the following : " & filename)
              '    End If
              '  Next j
              
                FileName = App.Path & "\Sample.swd"
              
                ' ********main code for listing all the channels that the house id watch*******
                            
                If fsys.FileExists(FileName) Then
                  MsgBox "file is open"
                Else
                  MsgBox "file not found"
                  Exit Sub
                End If
                
                Set txtstream = fsys.OpenTextFile(FileName, ForReading, False)
                Set outstream = fsys.OpenTextFile(App.Path & "\ChannelCount.txt", ForWriting, True)
                
                
                
                ' ****************************************
                ' Step 1: Build an array of all unique homeid/channel combinations
                ' ****************************************
                
                ArrayCount = 0
                Do Until txtstream.AtEndOfStream
                  SearchLine = txtstream.ReadLine
                  HomeAndChannel = Left(SearchLine, 10) & Mid(SearchLine, 13, 5)
                  ' Check whether we have already seen this combination.
                  Exists = False
                  For i = 1 To ArrayCount
                    If ChannelArray(i) = HomeAndChannel Then
                      Exists = True
                      Exit For
                    End If
                  Next
                  ' If not, add it to the array.
                  If Not Exists Then
                    ArrayCount = ArrayCount + 1
                    ChannelArray(ArrayCount) = HomeAndChannel
                  End If
                Loop
                
                
                
                ' ****************************************
                ' Step 2: Sort the array.
                ' ****************************************
                
                Dim k As Long
                Dim tmpsort As String
                
                For i = 1 To ArrayCount - 1
                  For j = ArrayCount - 1 To i Step -1
                    k = j + 1
                    If ChannelArray(j) > ChannelArray(k) Then
                      tmpsort = ChannelArray(j)
                      ChannelArray(j) = ChannelArray(k)
                      ChannelArray(k) = tmpsort
                    End If
                  Next
                Next
                
                
                
                ' ****************************************
                ' Step 3: Scan the array and report the number of channels per homeid
                ' ****************************************
                
                ChannelCount = 1
                PrevHomeId = Left$(ChannelArray(1), 10)
                For i = 2 To ArrayCount
                  HomeId = Left$(ChannelArray(i), 10)
                  If HomeId = PrevHomeId Then
                    ChannelCount = ChannelCount + 1
                  Else
                    outstream.WriteLine PrevHomeId & " " & Format(ChannelCount)
                    ChannelCount = 1
                    PrevHomeId = HomeId
                  End If
                Next
                ' Write out the last entry, which will otherwise be missed.
                outstream.WriteLine PrevHomeId & " " & Format(ChannelCount)
                outstream.Close
                txtstream.Close
                
                MsgBox "process completed"
              
              End Sub
              Thank you sir for ur time and patience that u put into my project.
              sir it is working fine.

              Now sir I have another version of this code which produces some what different results for unique channelcount.si r i m sending u the code if u have a look at it it would be wonderful sir.

              Code:
              Private Sub Command1_Click()
                  Dim MemberId As String
                  Dim Channel As String
                  Dim MemberChannel As String
                  Dim MemberChannelArray() As String
                  Dim Cnt As Integer
                  Dim X As String
                  Me.MousePointer = 11
                  Cnt = 0
                  Open App.Path & "\10117055.swd" For Input As #1
                  'This loop will store only one row for a member channel combination in an array
                  Do While Not EOF(1)
                      Line Input #1, X
                      MemberId = left(X, 10)
                      Channel = Mid(X, 13, 5)
                      MemberChannel = MemberId + Channel
                   
                      If Cnt = 0 Then
                          Cnt = Cnt + 1
                          ReDim Preserve MemberChannelArray(Cnt)
                          MemberChannelArray(Cnt) = MemberChannel
                      Else
                          If bsearcH(MemberChannelArray, MemberChannel, 1, Cnt) = -1 Then
                              Cnt = Cnt + 1
                              ReDim Preserve MemberChannelArray(Cnt)
                              MemberChannelArray(Cnt) = MemberChannel
                          End If
                      End If
                  Loop
                  Close #1
                  'Sorting the array
                  Call qsorT(MemberChannelArray, 1, Cnt)
                  Dim LastId As String
                  Dim i As Integer
                  Dim MemCnt As Integer
                  MemCnt = 1
                  LastId = left(MemberChannelArray(1), 10)
                  Open App.Path & "\output.txt" For Output As #2
                  For i = 2 To Cnt
                      If LastId <> left(MemberChannelArray(i), 10) Then
                          Print #2, LastId; " "; MemCnt
                          LastId = left(MemberChannelArray(i), 10)
                          MemCnt = 1
                      Else
                          MemCnt = MemCnt + 1
                      End If
                  Next
                  Print #2, LastId; " "; MemCnt
                  Close #2
                  Me.MousePointer = 0
              End Sub
              'Function for searching an item in an array using Binary Search Algorithm
              Private Function bsearcH(lsT() As String, sst As String, ByVal lB As Integer, ByVal uB As Integer) As Integer
                  Dim mD As Integer
                  Do While uB >= lB
                      mD = (uB + lB) / 2
                      If lsT(mD) = sst Then
                          bsearcH = mD
                          Exit Function
                      End If
                      If sst < lsT(mD) Then
                          uB = mD - 1
                      End If
                      If sst > lsT(mD) Then
                          lB = mD + 1
                      End If
                  Loop
                  bsearcH = -1
              End Function
              'Subroutine to perform a sort based on Quick Sort Algorithm
              Public Sub qsorT(in_array() As String, left As Integer, right As Integer)
                  Dim current As Integer, last As Integer
                  If left >= right Then
                      Exit Sub
                  End If
                  Call swaP(in_array, left, (left + right) / 2)
              
                  last = left
                  For current = left + 1 To right
                      If in_array(current) < in_array(left) Then
                            Call swaP(in_array, last, current)
                      End If
                  Next
                  Call swaP(in_array, left, last)
                  Call qsorT(in_array, left, last - 1)
                  Call qsorT(in_array, last + 1, right)
              
              End Sub
              
              Private Sub swaP(in_array() As String, i As Integer, j As Integer)
                  Dim temp As String
                  temp = in_array(i)
                  in_array(i) = in_array(j)
                  in_array(j) = temp
              End Sub
              now sir is the 2 different output produce by 2 programs for the same inputfile

              this is output generated
              by the new program

              0101100100 5
              0101100300 34 <------------------Notice the difference
              0101100400 10
              0101100500 3
              0101100600 10
              0101101000 26 <-------------------count over here

              this is output generated by the program given by u
              0101100100 5
              0101100300 11<-----------difference
              0101100400 10
              0101100500 3
              0101100600 10
              0101101000 16<-------count over here

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #37
                It would help if you told us which one is correct.

                One quick way would be to open your input file in MS Word, do a Find for "0101100300 " and choose "highlight all occurences". This will tell you how many times it finds it.

                Comment

                • Ronin
                  New Member
                  • Feb 2007
                  • 25

                  #38
                  Originally posted by Killer42
                  It would help if you told us which one is correct.

                  One quick way would be to open your input file in MS Word, do a Find for "0101100300 " and choose "highlight all occurences". This will tell you how many times it finds it.
                  sir the first one is correct the count is 34

                  Comment

                  • Killer42
                    Recognized Expert Expert
                    • Oct 2006
                    • 8429

                    #39
                    Originally posted by Killer42
                    It would help if you told us which one is correct.

                    One quick way would be to open your input file in MS Word, do a Find for "0101100300 " and choose "highlight all occurences". This will tell you how many times it finds it.
                    P.S. A binary search only works if the items are already in sorted sequence. This might explain the difference.

                    Comment

                    • Ronin
                      New Member
                      • Feb 2007
                      • 25

                      #40
                      Originally posted by Killer42
                      P.S. A binary search only works if the items are already in sorted sequence. This might explain the difference.
                      sir one thing i would like to mention here is that my inputfile that is the textfile
                      10117055.txt was already sorted,then why do we require a sorting code.

                      Comment

                      • Killer42
                        Recognized Expert Expert
                        • Oct 2006
                        • 8429

                        #41
                        Originally posted by Ronin
                        sir one thing i would like to mention here is that my inputfile that is the textfile
                        10117055.txt was already sorted,then why do we require a sorting code.
                        That's a good point. If you know definitely that the data will be sorted already, then obviously you don't need to bother sorting it. Removing the sort will simplify your code, which is a good thing.

                        I think the problem here is that you are producing a whole new program, then saying "why doesn't it work?". When debugging code you need to learn to break it down and test the individual parts first. For example, do you know whether your quicksort routine works? At what point do your results differ from mine? When the array is built, when it's sorted, or when it is counted up at the end?

                        It's very important to learn to change one thing at a time, so that if something breaks, you know what is it. Then it becomes much simpler to work out why/how.

                        (By the way. If you do need to sort, then the QuickSort routine is likely to save you tons of time compared to the simple bubble-type sort we've been using.)

                        P.S. If the data is already sorted, so you remove the sort and the bug goes away, then that will tell you where it was wrong. :)

                        Comment

                        • Ronin
                          New Member
                          • Feb 2007
                          • 25

                          #42
                          Originally posted by Killer42
                          That's a good point. If you know definitely that the data will be sorted already, then obviously you don't need to bother sorting it. Removing the sort will simplify your code, which is a good thing.

                          I think the problem here is that you are producing a whole new program, then saying "why doesn't it work?". When debugging code you need to learn to break it down and test the individual parts first. For example, do you know whether your quicksort routine works? At what point do your results differ from mine? When the array is built, when it's sorted, or when it is counted up at the end?

                          It's very important to learn to change one thing at a time, so that if something breaks, you know what is it. Then it becomes much simpler to work out why/how.

                          (By the way. If you do need to sort, then the QuickSort routine is likely to save you tons of time compared to the simple bubble-type sort we've been using.)

                          P.S. If the data is already sorted, so you remove the sort and the bug goes away, then that will tell you where it was wrong. :)
                          thank u sir for ur response will do that now.

                          Comment

                          • pongscript
                            New Member
                            • Mar 2007
                            • 27

                            #43
                            Originally posted by galahad
                            yep... you might be right about speed of processing... by how much an hour? but if you're developing and you have a deadline. do what is the fastest right? :D

                            it's just a suggestion. sometimes we trade processing time, for faster output and flexibility. and what if you have a new requirement for you report? then what? :D redo the program again? that's what i'm saying... if the SCRIPT can do it... then it'll be faster in the long run... in terms of producing it.

                            cheers.
                            Galahad,

                            since that problem was for text file.. why put database business on textfle business.. i mean if the operation doesnt require database

                            and since we dont know why he is using that kind of format, we dont know ... so why dont we stick to the plan... and just help that guy..

                            and for the speed .. every one love speed.. thats why we create program to gain speed...

                            Comment

                            Working...