-
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
-
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.
-
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.
-
ajirwin, try sort it out with the VBScript Regular Expression.
-
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
-
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
-
Code:
iCnt(Asc(Mid$(sRec, , i, 1))) = iCnt(Asc(Mid$(sRec, i, 1))) + 1
^^^
I think there is too many commas here...
-
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.