Results 1 to 8 of 8

Thread: Counting characters in a File

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2001
    Location
    Toronto
    Posts
    103

    Question

    Using VB6. I have created a file (thanks to all the help on hte early question, worked) now I need to count the number of characters in the file and the number of time each character appers in the file to compare it to a users input. The file is made up of the ASCII characters between 33 and 126 at random, I need to vefify that the character is in the file the correct number of times.


    Thanks

  2. #2
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    Open the file as Binary. Then read it 1 character at a time into a Byte, or create a byte array of size n and you can read n bytes at a time. You will have to step through the array after each read.

    Then as you read each byte, the value of the byte will be the index value of the array element that you want to increment.

    for example:
    Code:
    Dim aryCount(126) as Integer
    Dim bytChar           as Byte
    
    Open "myfile" for Binary as #1
    Do While Not EOF(1)
       Read #1, bytChar
       aryCount(bytChar) = aryCount(bytChar) + 1  
    Loop
    I'm assuming you can use the byte as an index, I have never tried this in VB. Otherwise you will have to assign the value to an integer.

    VB may even let you dim the array as:
    Code:
    Dim aryCount(33 To 126) as Integer
    I have done this in Pascal in the past.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2001
    Location
    Toronto
    Posts
    103

    More Help Needed

    the "Read #1, bytChar" does not work I get "Expected: Expression", I tryed the Get command and i get Runtime Error '6': Overflow. The line aryCount(bytChar)=32767 and I beleve it is only set for 126. Does anyone have any suggestion on how to fix this.

  4. #4
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    ajirwin, try sort it out with the VBScript Regular Expression.

  5. #5
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    Okay, it's obvious I don't do a lot of file I/O in VB.

    Drop the following into a form that has a Listbox on it. It's simple but it worked. It should get you on the right path.
    Code:
    Option Explicit
    
    Private Sub Form_Load()
    Dim iCnt(126) As Integer
    Dim i         As Integer
    Dim sRec      As String
    
    Open "your file goes here" For Input As #1
    Do While Not EOF(1)
       Line Input #1, sRec
       For i = 1 To Len(sRec)
           iCnt(Asc(Mid$(sRec, i, 1))) = iCnt(Asc(Mid$(sRec, i, 1))) + 1
       Next i
    Loop
    Close #1
    For i = 33 To 126
        List1.AddItem Chr(i) & " = " & iCnt(i)
    Next i
    
    End Sub

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2001
    Location
    Toronto
    Posts
    103

    Unhappy

    I keep getting the message "Complie error : Argument not optional" and it has this line highlighted "iCnt(Asc(Mid$(sRec, , i, 1))) = iCnt(Asc(Mid$(sRec, i, 1))) + 1". My VB skills are really rust so do I appecate all the help.


    Thanks

  7. #7
    Lively Member
    Join Date
    Aug 2000
    Location
    Ontario, Canada
    Posts
    79
    Code:
    iCnt(Asc(Mid$(sRec, , i, 1))) = iCnt(Asc(Mid$(sRec, i, 1))) + 1
                       ^^^
    I think there is too many commas here...

  8. #8
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    CoreyS is correct. You must have typed the code instead of doing a Copy&Paste.

    If you check the code in my post you will see that there is no double comma between sRec and i.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width