Outline ·
[ Standard ] ·
Linear+
Counting letter, how to count letter from input text file
|
TSdeehanie
|
Mar 3 2006, 10:03 PM, updated 20y ago
|
New Member
|
Basically, i have done for counting letter(how many times 'a' occured) from string in textbox.But how if i want to count letter such that from a text file? I've been using vb for this:The code below just used to count characters, not a single letter..Can someone show me how?thanks in advance!
Dim FNum As Integer Dim FBuf() As Byte Dim CharCount(255) As Long Dim LoopChar As Long
Const FileName As String = "X:\Path\File.xyz"
FNum = FreeFile() ' Open file and read contents Open FileName For Binary Access Read Lock Write As #FNum ReDim FBuf(LOF(FNum) - 1) As Byte Get #FNum, , FBuf() Close #FNum
For LoopChar = 0 To UBound(FBuf()) ' Create character counts CharCount(FBuf(LoopChar)) = CharCount(FBuf(LoopChar)) + 1 Next LoopChar
This post has been edited by deehanie: Mar 3 2006, 10:08 PM
|
|
|
|
|
|
asc2
|
Mar 3 2006, 10:39 PM
|
|
count a - z the times they occur in a text file or count how many words in a text file
This post has been edited by asc2: Mar 3 2006, 10:39 PM
|
|
|
|
|
|
silverhawk
|
Mar 4 2006, 01:14 AM
|
Eyes on Target
|
Read from the file and put the contents into a string variable and read it the same way you would from a textbox.
|
|
|
|
|
|
wKkaY
|
Mar 4 2006, 07:13 AM
|
misutā supākoru
|
QUOTE ReDim FBuf(LOF(FNum) - 1) As Byte Get #FNum, , FBuf() This is bad practice. You will end up reading the whole file into memory - which poses a problem if the system doesn't have enough RAM available. Read and process chunks of data at a time instead. This post has been edited by wKkaY: Mar 4 2006, 07:13 AM
|
|
|
|
|
|
anthony_yio
|
Mar 4 2006, 10:59 AM
|
........
|
QUOTE(wKkaY @ Mar 4 2006, 07:13 AM) This is bad practice. You will end up reading the whole file into memory - which poses a problem if the system doesn't have enough RAM available. Read and process chunks of data at a time instead. Actually, there is no worry for modern day OS, system will proceed to virtual memory. But that is only for counting number of char, lol.
|
|
|
|
|
|
asc2
|
Mar 4 2006, 11:27 AM
|
|
if you wanna count number occurance of a character in a file
1. have a hashmap something like a 2 dimensional array 2. read from the file example 512 characters at a time 3. for each character look in the hashmap for its existence(key) 4. if it doesn't exist, add the character to the hashmap and +1 to its value, if it already exist, +1 to its value 5. when all the characters in the file are processed, you'll have a hashmap with all the characters in the file and their count
This post has been edited by asc2: Mar 4 2006, 11:29 AM
|
|
|
|
|
|
TSdeehanie
|
Mar 4 2006, 04:33 PM
|
New Member
|
QUOTE(silverhawk @ Mar 4 2006, 01:14 AM) Read from the file and put the contents into a string variable and read it the same way you would from a textbox. I've tried this and its work! Thanks .. but the problem is my counting result(i've used listbox to display) and it still counting the previous value of the previous textbox input. I've put the clear button to clear all the listed result( lstResult.Clear) but i guess it's only clear what is in the listbox, not to reset the previous value.
|
|
|
|
|
|
wKkaY
|
Mar 4 2006, 05:17 PM
|
misutā supākoru
|
QUOTE(anthony_yio @ Mar 4 2006, 01:59 PM) Actually, there is no worry for modern day OS, system will proceed to virtual memory. But that is only for counting number of char, lol. Yes, but paging to virtual memory is the worst thing that can happen for this application, as you're trying to read from disk and at the same time paging out! Why let yourself be memory-bound when you don't have to?
|
|
|
|
|
|
anthony_yio
|
Mar 4 2006, 05:29 PM
|
........
|
QUOTE(wKkaY @ Mar 4 2006, 05:17 PM) Yes, but paging to virtual memory is the worst thing that can happen for this application, as you're trying to read from disk and at the same time paging out! Why let yourself be memory-bound when you don't have to?  Of course I know that. That is why I end my reply with lol (laugh out loud) (sarcastically) Many programs nowadays "can" run without much problems even if they leak or inefficiently been programmed. Modern OS take care of all the problems for them, unlike those Win9x days that require a restart of OS if the program leak and etc. Now, all they need to do is just close the application and start again (for leak), all the memory get reclaimed. Even if they use up all the memory, OS still take care of the mess with virtual memory. And what next is the .NET with a garbage collector. Even ppl with zero knowledge on memory paging concept can program now. lol
|
|
|
|
|
|
wKkaY
|
Mar 4 2006, 09:56 PM
|
misutā supākoru
|
Righto, so let's declare all our string variables a gigabyte long even if they're to store a single character, because the OS handles them "without much problem".
|
|
|
|
|
|
anthony_yio
|
Mar 6 2006, 10:54 AM
|
........
|
QUOTE(wKkaY @ Mar 4 2006, 09:56 PM) Righto, so let's declare all our string variables a gigabyte long even if they're to store a single character, because the OS handles them "without much problem". Err, I was just trying to explain why things still run that way. (eventhough it is not efficient) Programming in old days will be "forced" to make things run as efficient as possible because they are clearly out of choices. (hardware spec was low those days. ) But now, the PC is so powerful that literally, anything goes even without the understanding in programming. That is why, I was trying to explain why it will still work "without much problems" but of course again, it is not encouraged.
|
|
|
|
|
|
SincerePrayer
|
Mar 7 2006, 11:22 AM
|
love to pray
|
wKkaY & anthony_yio, You guys both got the point. The design of the program is not efficient but is still workable. Let's try not to give deehanie a freak.  deehanie, You might want to reset the array and the variables that hold any values during the search. Happy Coding and Sharing
|
|
|
|
|