Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Count Characters; Words and Lines in a Text File 2

Status
Not open for further replies.

Dauphy

IS-IT--Management
Nov 8, 2001
111
CA
Hi!

I need to know how to count the number of characters (don't include spaces); the number of words and the number of lines in the same text file. I'm 'clueless' on this one. About all I know is how to read it :)

Thanks!!!!
 
Only one way I know of....character by character. There used to be a LineInput function that would read a line from a text file, and you could read the entire file line by line, but I don't know if it is still supported. If so, you can read a line, then increment a line counter. Reading the line sets the line equal to a string variable. Then, scan each character in the line, and increment a character counter until you came to a space. When you come to a space, increment a word counter. You will have to check for double spaces, such as at the end of a sentence, otherwise you would count the second space as another word. Also, I don't know if you would have to figure out like split words at the end of a line, things like that. Let me know if this makes sense to you or not, and I'll try to be more specific. I'll also try to find the syntax for that LineInput function.
 

Give this a try.

[tt]

Dim FName As String, FNumb As Integer, TextLine As String
Dim CharCount As Double, WordCount As Double, P As Double
Dim LineCount As Double

FName = "Your path file name"
FNumb = FreeFile
FName = "C:\z\test.txt"

Open FName For Input As #FNumb

Do While Not EOF(FNumb)
Line Input #FNumb, TextLine
CharCount = CharCount + Len(Replace(TextLine, " ", ""))

TextLine = TextLine & " "

TextLine = Replace(TextLine, " ", " ")

P = InStr(1, TextLine, " ")
If P > 0 Then WordCount = WordCount + 1

Do While P > 0
P = InStr(P + 1, TextLine, " ")
If P > 0 Then WordCount = WordCount + 1
Loop


LineCount = LineCount + 1

Loop

Close #FNumb

Debug.Print "Characters = " & CharCount
Debug.Print "Words = " & WordCount
Debug.Print "Lines = " & LineCount


[/tt]

Good Luck
 
Thanks to both of you ... I've read about the LineInput function but I hadn't had a chance to try it; I still will. vb5prgrmr; worked like a charm!!!!

 
For those who are interested in severe alternatives, the following bit of code and sample text file are offered. For trivial and smallish files, this is, perhaps, more complex than necessary. For larger or more frequent use, line input is one of the slower operations available in VB, so its avoidance is useful. The instancing of the SPLIT() function, while extremely convenient, is also a bt of a resource hog. I have used it here, as the slowdown for it's use is much more than offset by the increase of speed obtained from loading the entire file into memory (even if some of it is virtual!).

Some, from the name alone, may guess that the overall process is (minimally) adapted from a routine used to import CSV files into other structures. I often use this approach to get (rather messy) CSV files into an array structure for V&V operations prior to appending them to a data table.

It should also be obvious that -as written- the routines are NOT suitable for applications to general text files. The initial process does obtain the correct number of lines, however the number of "words" in a line is NOT suitable, as it is simply the number of words in the FIRST line, times the number of "lines" in the input file. A simple modification (add a loop to "Split()" each line and accumulate the number of words) would easily solve the Number of words accurately, while the same loop could count and accumulate the number of characters in each word and line.

It should aslo be noted that the use of the split function will result in the exclusion of all end of line 'characters' and the the count of the space characters, but will not exclude other "white space" from the statstics (Tab, Bell, ...).




Code:
Public Function basCSV2Array(FName As String, _
                             Optional RecSep As String = vbCrLf, _
                             Optional FldSep As String = ",") As Variant

    Dim Fil As Integer
    Dim NRecs As Long
    Dim NFlds As Long
    Dim Idx As Long
    Dim Jdx As Integer
    Dim J As Long                               'indices
    Dim RawFile As String                       'Holds the entire contents of the file
    Dim RawSplit() As String                    'the file split up on a line per line basis
    Dim OneLine() As String                     'used to hold the tokens for one line
    Dim RptAry() As String                      '2d array. Holds X lines & N elements per line

    Fil = FreeFile                              'get the next free file number

    'This works for large files. I (Troy Williams)tried it
    'with a 50 meg file on a computer with 128 Mb of ram and it worked fine.
    'open the file and dump the contents into the rawfile variable

    Open FName For Binary As #Fil               'Open file
    RawFile = String$(LOF(Fil), 32)             'Create "empty" String of Length
    Get #Fil, 1, RawFile                        'Fill "Empty Str with File
    Close #Fil                                  'Close File

    'Get the Nunber of Records and Fields
    RawSplit = Split(RawFile, RecSep)           'Split the file up by lines
    NRecs = UBound(RawSplit) - 1                'Num Lines/Records in the file

    OneLine = Split(RawSplit(0), FldSep)        'Split the first line
    NFlds = UBound(OneLine)                     'Get the Number of Fields

    ReDim RptAry(0 To NRecs, 0 To NFlds)

    For Idx = 0 To NRecs
       OneLine = Split(RawSplit(Idx), FldSep)
       For Jdx = 0 To NFlds
          RptAry(Idx, Jdx) = OneLine(Jdx)
       Next Jdx
    Next Idx

    basCSV2Array = RptAry

End Function
Public Function basTstCSV2Array()

    Dim Fil As Integer
    Dim Idx As Long
    Dim Jdx As Long
    Dim MyLOF As Long
    Dim FilNme As String
    Dim MyArray As Variant

    FilNme = "C:\My Documents\MyCsvFile.Txt"

    Fil = FreeFile
    Open FilNme For Binary As #Fil               'Open file
    MyLOF = LOF(Fil)
    Close #Fil                                  'Close File


    MyArray = basCSV2Array(FilNme, , " ")
    
    Idx = UBound(MyArray, 1)
    Jdx = UBound(MyArray, 2)

'    For Kdx = 0 To Idx
'        For Ldx = 0 To Jdx
'            Debug.Print MyArray(Kdx, Ldx);
'        Next Ldx
'        Debug.Print
'    Next Kdx

    Debug.Print "# Lines = " & UBound(MyArray, 1) + 1
    Debug.Print "# Words = " & (UBound(MyArray, 1) + 1) * (UBound(MyArray, 2) + 1)
    Debug.Print "# Chars = " & MyLOF - ((UBound(MyArray, 1) + 1) * (UBound(MyArray, 2) + 1))

    '? basTstCSV2Array
    '# Lines = 2
    '# Words = 16
    '# Chars = 90



End Function

the text file
Code:
123456, 08/15/2001, 2:39:40 AM, 2:41:29 AM, 49 secs
123654, 08/15/2001, 2:49:40 AM, 2:51:29 AM, 49 secs
MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
This does about the same. The word count is similar to MS Word's word count:

Option Explicit
Public Type udtWordCount
lChars As Long
lWords As Long
lLines As Long
End Type

Public Sub FileWordCount(FileName As String, ByRef WordCount As udtWordCount)

Dim strChar As String
Dim strLastChar As String
Dim FileNum As Integer
Dim Char As Integer

FileNum = FreeFile
Open FileName For Input As #FileNum
With WordCount
.lChars = 0
.lWords = 0
.lLines = 0

Do While Not EOF(FileNum)
strChar = Input(1, #FileNum)
Char = Asc(strChar)

Select Case Char
Case 13
.lLines = .lLines + 1
If Asc(strLastChar) > 32 Then .lWords = .lWords + 1
Case 32
If Asc(strLastChar) > 32 Then .lWords = .lWords + 1
Case Is > 32
.lChars = .lChars + 1
Case Else

End Select
strLastChar = strChar
Loop
End With
Close #FileNum

End Sub [/b][/i][/u]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top