text file operations with vb6.0

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ronin
    New Member
    • Feb 2007
    • 25

    #16
    Originally posted by Killer42
    Well, you certainly do have some interesting logic there.

    I don't have a lot of time right now, so I've banged together a modified version that I hope will help you with the process. I have not tested it, as I don't have any test data and can't spare the time to create some. Anyway, after making sure you've got a backup copy of your current code (in case mine makes things worse) just replace the contents of the cmdclick_Click routine with this, and see how it goes...
    Code:
      Dim j As Integer
      Dim searchline As String
      Dim filename As String
      Dim homeid As String
      Dim prevhomeid As String
      Dim channelname As String
      Dim channelcount As Integer
      Dim HomeAndChannel As String  ' <-- Added this variable.
      Dim Exists As Boolean         ' <-- And this one.
      Dim ChannelsToReport As Long  ' <-- This one, too.
      Dim channelarray(0 To 100000) As String
      ' Dim newchannelarray(0 To 100000) As String
      ' Dim carray As String
      ' Dim prevcarray As String
      'Dim i As Double
      Dim i As Long
    
      ' 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
                   
      '********main code for listing all the channels that the house id watch*******
                  
      If fsys.FileExists("C:\rohit program\exercise\SWD\" & filename) = True Then
        MsgBox "file is open"
      Else
        MsgBox "file not found"
        Exit Sub
      End If
      'prevcarray = ""
      'prevchannelname = ""
      'prevhomeid = ""
      channelcount = 0
      'prevchannelarray(k) = ""
      Set outstream = fsys.OpenTextFile("c:\channelcount.txt", ForWriting, True)
      Set txtstream = fsys.OpenTextFile("C:\rohit program\exercise\SWD\" & filename, ForReading, False)
      
      
      
      ' ****************************************
      ' Step 1: Build an array of all unique homeid/channel combinations
      ' ****************************************
      
      Do Until txtstream.AtEndOfStream
        searchline = txtstream.ReadLine
        homeid = Mid(searchline, 1, 10)
        channelname = Mid(searchline, 13, 5)
        'channelarray(i) = homeid & channelname
        HomeAndChannel = homeid & channelname
      
      
        ' Check whether we have already seen this combination...
        Exists = False
        For i = 1 To channelcount
          If channelarray(i) = HomeAndChannel Then
            Exists = True
            Exit For
          End If
        Next
        
        ' If not, add it to the array.
        If Not Exists Then
          channelcount = channelcount + 1
          channelarray(channelcount) = HomeAndChannel
        End If
      Loop
      
      
      
      ' ****************************************
      ' Step 2: Sort the array. I'll leave this to you, as I'm short on time
      ' ****************************************
      
      
      
      
      
      ' ****************************************
      ' Step 3: Scan the array and report the number of channels per homeid
      ' ****************************************
      
      ChannelsToReport = 1
      prevhomeid = Left$(channelarray(1), 10)
      For i = 2 To channelcount
        homeid = Left$(channelarray(i), 10)
        If homeid = prevhomeid Then
          ChannelsToReport = ChannelsToReport + 1
        Else
          outstream.WriteLine prevhomeid & " " & Format(ChannelsToReport)
          ChannelsToReport = 1
          prevhomeid = homeid
        End If
      Next
      MsgBox "process completed"
    Thanks for your prompt response sir

    Although it was very helpful i did not have the desired results.

    now according to u i have to sort the array in step 2 here is how i am doing it using bubble sort ,but it is giving me an error.


    Step 2 to sort array

    Code:
    'sorting of array
                               For i = LBound(channelarray) To UBound(channelarray) - 1
                                 For k = i + 1 To Ubound(channelarray) - 1 <-------- this step gives error overflow.
                                  If channelarray(i) > channelarray(k) Then
                                    tmpsort = channelarray(i)
                                    channelarray(i) = channelarray(k)
                                    channelarray(k) = tmpsort
                                  End If
                                 Next
                                Next

    As per the output if i dont sort it the output has something like this

    0101100300 1
    0101100100 4
    0101100300 1
    0101100100 4
    0101100300 1
    0101100100 4
    0101100300 11
    0101100400 1
    0101100100 4
    0101100300 11
    0101100400 1
    0101100100 4

    although some count are right some are wrong,it is giving me the count of homeid suppose if homeid has repeated 11 time then it gives
    0101100300 11
    as u can see sir,anyways sir pls tell why array sort is giving me error.
    and what changes should i make.

    Thank u sir for quick response ,effort and time .

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #17
      The first thing I would do is move the sort out to a separate Sub to avoid interfering with your existing processing. That also gives you a generic sort routine that you can use for other things.

      One question - what is k defined as? If it's Integer, it can only go up to 32768, or somewhere around there. I normally use Long, for that reason and also for reasons of performance (it's the "native" data type on 32-bit processors so you save time on conversions).

      (I've just started work. Won't have time to look at your code until lunchtime, sorry.)

      Comment

      • Ronin
        New Member
        • Feb 2007
        • 25

        #18
        Originally posted by Killer42
        The first thing I would do is move the sort out to a separate Sub to avoid interfering with your existing processing. That also gives you a generic sort routine that you can use for other things.

        One question - what is k defined as? If it's Integer, it can only go up to 32768, or somewhere around there. I normally use Long, for that reason and also for reasons of performance (it's the "native" data type on 32-bit processors so you save time on conversions).

        (I've just started work. Won't have time to look at your code until lunchtime, sorry.)
        Oh no problem sir, One thing i would like to mention u about your code is that
        it gives the results but suppose if my textfiles contains 98873 lines of data.
        the output file has around over 2 lakhs lines of data. and the output file
        keeps on repeating the homeid again and again ,from this i mean that
        suppose homeid 0007100200 has watched 1 unique channels
        this is repeated quite a number of times,why is this happening.

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #19
          Originally posted by Ronin
          Oh no problem sir, One thing i would like to mention u about your code is that
          it gives the results but suppose if my textfiles contains 98873 lines of data.
          the output file has around over 2 lakhs lines of data. and the output file
          keeps on repeating the homeid again and again ,from this i mean that
          suppose homeid 0007100200 has watched 1 unique channels
          this is repeated quite a number of times,why is this happening.
          Good question.

          I'm home now, so I can take a bit of time to go over the code. Stay tuned...

          Um... what's a lakh?

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #20
            Ok, I think there's a bug in your sort.

            The code looks good (did you manage to resolve the overflow error?) but I think you need to make one small change. You have the right idea, but I think you are only causing things to "bubble up" part of the way. Try this...
            Code:
            Change this line...
              For k = i + 1 To Ubound(channelarray) - 1
            to...
              For k = Ubound(channelarray) - 1 To i Step -1
            I'm not going to look at the rest of the code just yet, because I think that fixing the sort may resolve your problem.

            Comment

            • Ronin
              New Member
              • Feb 2007
              • 25

              #21
              Originally posted by Killer42
              Ok, I think there's a bug in your sort.

              The code looks good (did you manage to resolve the overflow error?) but I think you need to make one small change. You have the right idea, but I think you are only causing things to "bubble up" part of the way. Try this...
              Code:
              Change this line...
                For k = i + 1 To Ubound(channelarray) - 1
              to...
                For k = Ubound(channelarray) - 1 To i Step -1
              I'm not going to look at the rest of the code just yet, because I think that fixing the sort may resolve your problem.
              i mean that the text file has only around 9000 lines of data and the output txtfile that is generated is over 1lakh lines of data. i mean homeid keeps on repeating and repeating.

              Comment

              • Ronin
                New Member
                • Feb 2007
                • 25

                #22
                Originally posted by Ronin
                i mean that the text file has only around 9000 lines of data and the output txtfile that is generated is over 1lakh lines of data. i mean homeid keeps on repeating and repeating.
                sir once i write the sorting array code tha entire project just hangs .why does this happen

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #23
                  Originally posted by Ronin
                  sir once i write the sorting array code tha entire project just hangs .why does this happen
                  Without seeing the whole of the code, it's difficult to say. But probably you (or I) have managed to create an infinite loop there somewhere.

                  Did you move the sort out to a separate Sub and pass the array to it? I think that will be a lot safer. Modular code like that is also much easier to debug.

                  And I still don't know what "1lakh" means.

                  Comment

                  • Ronin
                    New Member
                    • Feb 2007
                    • 25

                    #24
                    Originally posted by Killer42
                    Without seeing the whole of the code, it's difficult to say. But probably you (or I) have managed to create an infinite loop there somewhere.

                    Did you move the sort out to a separate Sub and pass the array to it? I think that will be a lot safer. Modular code like that is also much easier to debug.

                    And I still don't know what "1lakh" means.
                    sir is it possible that i can send u a sample text file so that u can get a clear
                    picture of what i m talkin about.in this way u will have the sample txtfile to test or else sir if u want i can send the entire project.and yes sir i tried to do that
                    but was very unsucessful.

                    Comment

                    • Ronin
                      New Member
                      • Feb 2007
                      • 25

                      #25
                      Originally posted by Killer42
                      Without seeing the whole of the code, it's difficult to say. But probably you (or I) have managed to create an infinite loop there somewhere.

                      Did you move the sort out to a separate Sub and pass the array to it? I think that will be a lot safer. Modular code like that is also much easier to debug.

                      And I still don't know what "1lakh" means.
                      and sir I m sending the entire code again in the way i m doing it,hope it helps

                      Code:
                      Option Explicit
                      Dim fsys As New FileSystemObject
                      Dim txtstream As TextStream
                      Dim outstream As TextStream
                      
                      
                      
                      Public Sub cmdclick_Click()
                       Dim j As Integer
                       Dim searchline As String
                       Dim filename As String
                       Dim homeid As String
                       Dim prevhomeid As String
                       Dim channelname As String
                       Dim channelcount As Integer
                       Dim channelarray(0 To 100000) As String
                       Dim HomeAndChannel As String
                       Dim Exists As Boolean
                       Dim ChannelsToReport As Long
                       Dim carray As String
                       Dim prevcarray As String
                       Dim i As Double
                       Dim k As Double
                       Dim tmpsort As String
                       
                       
                       
                       
                       
                         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
                                   
                        '********main code for listing all the channels that the house id watch*******
                                  
                                   If fsys.FileExists("C:\rohit program\exercise\SWD\" & filename) = True Then
                                     MsgBox "file is open"
                                   Else
                                     MsgBox "file not found"
                                   End If
                                                   channelcount = 0
                                       Set outstream = fsys.OpenTextFile("c:\channelcount.txt", ForWriting, True)
                           Set txtstream = fsys.OpenTextFile("C:\rohit program\exercise\SWD\" & filename, ForReading, False)
                                     Do Until txtstream.AtEndOfStream
                                         searchline = txtstream.ReadLine
                                           homeid = Mid(searchline, 1, 10)
                                            channelname = Mid(searchline, 13, 5)
                                              channelarray(i) = homeid & channelname
                                               HomeAndChannel = homeid & channelname
                                                     Exists = False
                                                  For i = 0 To channelcount
                                                      If channelarray(i) = HomeAndChannel Then
                                                      Exists = True
                                                  Exit For
                                                      End If
                                     
                                                  Next
                                       
                                                     If Not Exists Then
                                                       channelcount = channelcount + 1
                                                       channelarray(channelcount) = HomeAndChannel
                                                       
                                     Loop
                                     
                           [B]       'sorting array<----------- code for sorting[/b]
                                   
                                                  For i = LBound(channelarray) To UBound(channelarray) - 1
                                                   For k = UBound(channelarray) - 1 To i Step -1
                                                     If channelarray(i) > channelarray(k) Then
                                                        tmpsort = channelarray(k)
                                                        channelarray(k) = channelarray(i)
                                                        channelarray(i) = tmpsort
                                                     End If
                                                  Next
                                              Next
                                                     
                                                     
                                   
                                                       ChannelsToReport = 1
                                                       prevhomeid = Left$(channelarray(i), 10)
                                                                  For i = 1 To channelcount
                                                                    homeid = Left$(channelarray(i), 10)
                                                                      If homeid = prevhomeid Then
                                                                           ChannelsToReport = ChannelsToReport + 1
                                                                      Else
                                                                             outstream.WriteLine prevhomeid & " " & Format(ChannelsToReport)
                                                                             ChannelsToReport = 1
                                                                             prevhomeid = homeid
                                              
                                                                      End If
                                                                  Next
                                                     End If
                               
                               'Loop<---am i right in putting this loop here
                                         MsgBox "process completed"
                                        
                      End Sub
                      sir 1 lakh is the count of number of lines that is produced after the execution of
                      the program,it is surprising bcoz the file that i am using as inpu has aroun 9884
                      homeids so output can never be that large.

                      Comment

                      • Ronin
                        New Member
                        • Feb 2007
                        • 25

                        #26
                        Originally posted by Killer42
                        Without seeing the whole of the code, it's difficult to say. But probably you (or I) have managed to create an infinite loop there somewhere.

                        Did you move the sort out to a separate Sub and pass the array to it? I think that will be a lot safer. Modular code like that is also much easier to debug.

                        And I still don't know what "1lakh" means.
                        Here is the output tht it creates
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100100 5
                        0101100300 11
                        0101100100 5
                        0101100300 11
                        0101100100 5
                        0101100300 11
                        0101100100 5
                        0101100300 11
                        0101100100 5
                        0101100300 11
                        0101100100 5
                        0101100300 11
                        0101100100 5
                        0101100300 11
                        0101100100 5
                        0101100300 11
                        0101100100 5
                        0101100300 11
                        0101100100 5
                        0101100300 11
                        0101100100 5
                        0101100300 11

                        it creates redundant rows in the output file.
                        and hence the size of output file increases.

                        Comment

                        • Killer42
                          Recognized Expert Expert
                          • Oct 2006
                          • 8429

                          #27
                          Originally posted by Ronin
                          Code:
                          ...
                          sir 1 lakh is the count of number of lines that is produced after the execution of the program,it is surprising bcoz the file that i am using as inpu has aroun 9884 homeids so output can never be that large.
                          Ok, I finally managed to track down "lakh" via Google. So, it's a hundred thousand, huh? Where is this term used? I've never heard it before.

                          That code does not compile. There's a block IF without an END IF, just before the sorting code. So this can't be the code you are executing.

                          I still think that the sorting is probably behind any further problems. So in my opinion, you should ignore (or even comment out) the subsequent code and concentrate on the sort to make sure it works, before moving on. I'd suggest you cut down one of your data files to ... oh, say a hundred records. Then read that in, see what is in your array, then run it through the sort routine and see what comes out.

                          Also, a couple of observations on the code...
                          • I'd recommend reducing the indenting, and tidying it up so it makes a bit more sense. The idea is to indent things to show how they are logically nested within other structures. You appear to have indented each section of code further than the last, so that it all goes way off the side and one has to scroll over to see it.
                          • When building the array, I used
                            For i = 1 To channelcount
                            While you used
                            For i = 0 To channelcount
                            I’m not sure how significant the difference is, but in programming you really have to watch the little details, or they’ll bite you.
                          • I commented out the line
                            channelarray(i) = homeid & channelname
                            while you still have it there. I’m not sure how significant this is, since i is zero at this point.
                          • I think I found your missing End If, down the bottom just above the MsgBox statement.

                          Comment

                          • pongscript
                            New Member
                            • Mar 2007
                            • 27

                            #28
                            you could use selectunique() and specify your filename
                            then it would return and array of DataEntry to write it to file call WriteData() and specify
                            you new filename and Dataentry Array

                            as you can see there no error handler, so its up to you to add these feature




                            [HTML]

                            publictype Dataentry
                            HomeID as string
                            UniqueChannels as long
                            channels() as string 'you could put * 5 to limit character
                            end type


                            public function selectuniques(b yval filename as string) as DataEntry()
                            dim retval() as string 'temporary variable for return
                            dim temp as string 'temporary variable for line read
                            dim fr as int 'file pointer
                            fr = freefile() 'get free file pointer
                            open filename for input as fr 'open file(input means read)
                            while not Eof(fr) 'loop until end of file
                            input line fr,temp 'read line
                            PushData(mid(te mp,1,10) ,mid(temp,12,5) ,retal()) 'Call the Pushdata - which checks and add uniques
                            wend
                            close fr 'close the file
                            selectuniques = retval()
                            end sub

                            public sub WriteData(byval filename as string,_entries as DataEntry())
                            dim fr as int
                            dim temp as string
                            fr = freefile()
                            open filename for output as fr
                            for i = 0 to ubound(_entries )
                            temp = _entries(i).Hom eid & " " & _entries(i).uni quechannels
                            print fr,temp
                            next
                            close fr
                            end sub

                            private sub PushData(byval _homeId as string,_Channel name as string,byref _entrylist as DataEntry())
                            dim found as boolean,channel found as boolean
                            dim _index as long
                            found =false
                            channelfound =false

                            if ubound(_entryli st)=0 then 'check where _entrylist where empty array
                            redim _entrylist(1) as DataEntry
                            _entrylist(0).H omeId = _homeId
                            redim _entrylist(0).C hannels(1) as string
                            _entrylist(0).c hannels(0) = _channelname
                            _entrylist(0).U niquechannel = 1
                            else

                            for i = 0 to ubound(_entryli st) 'loop throughout the entries

                            if _newentry = _entrylist(i).h omeid then 'check whether the homeid exist

                            if ubound(_entryli st(i).channels) = 0 then 'check whether the channel list where empty, if empty add new channel
                            redim Preserve _entrylist(i).c hannels(1) as string
                            _entrylist(i).u niquechannnels = 1
                            _entrylist(i).c hannels(0) = _channelname
                            else
                            for j=0 to ubound(_entryli st(i).channels)
                            if _entrylist(i).c hannels(j) = _channelname then
                            channelfound =true
                            exit for
                            end if
                            next
                            end if
                            _index = i
                            found = true
                            Exit for 'if home id were found then exit the for loop
                            end if
                            next
                            if not found then 'when home id was not found then add it to DataEntry array
                            redim preserve _entrylist(ubou nd(_entrylist)+ 1) as DataEntry
                            _entrylist(ubou nd(_entrylist). Homeid = _homeid
                            _entrylist(ubou nd(_entrylist). uniquechannels= _entrylist(ubou nd(_entrylist). uniquechannels + 1 'increment unique

                            '
                            redim Preserve _entrylist(ubou nd(_entrylist)) .channels(1) as string
                            _entrylist(ubou nd(_entrylist)) .channels(0) = _channelname
                            elseif not channelfound then
                            redim preserver _entrylist(i).c hannels(ubound( _entrylist(i).c hannels)+1) as string
                            _entrylist(i).u niquechannels = _entrylist(i).u niquechannels + 1
                            _entrylist(i).c hannels(ubound( _entrylist(i).c hannels)) = _channelname
                            end if

                            end if
                            end sub
                            [/HTML]

                            Comment

                            • pongscript
                              New Member
                              • Mar 2007
                              • 27

                              #29
                              by the way if you will copy it to a form just change the type to private

                              or

                              just put in on a module(most efficient)

                              just email me @ Removed by Moderator if you find these suck or you want a breif explanation


                              by the way i havent test that code... because i dont have a vb6 in my computer right now, cos i already migrated to other language like C# .
                              Last edited by Killer42; Mar 29 '07, 01:20 AM. Reason: Removed e-mail address.

                              Comment

                              • galahad
                                New Member
                                • Mar 2007
                                • 2

                                #30
                                you know, it would have been easier if you transfer that textfile to a temporary database file like MS Access and use queries to get your desired output.

                                you're output is simple. And and SQL script using DISTINCT would do the trick. It seems you've been using the long method.

                                Comment

                                Working...