Having trouble with reading characters of a file

markp15
05-28-2004, 02:16 PM
I have written a program that is supposed to read the number of lines in a file and the number of characters in that file. The line count works but the number count is way off. Can't figure out what I have done wrong, any help would be great.Dim file As String
Dim textline As String
Dim counter As Integer

Private Sub cmdexit_Click()

'Exit the program
Call Unload(frmstats)


End Sub

Private Sub cmdstatistics_Click()

'Count the lines and letters in the file

counter = 0

Open "C:\" & txtfilename.text For Input As #1
Do While Not EOF(1)
Line Input #1, textline

counter = counter + 1
lbllinesanswer.Caption = counter
Loop
Close #1

'It's this Part that does not work right

Open "C:\" & txtfilename.text For Input As #1
Line Input #1, file

lblcharanswer.Caption = Len(file)

Close #1

End SubThanks
Mark

KAL_0801
05-28-2004, 02:34 PM
The problem is that in this code:

Open "C:\" & txtfilename.text For Input As #1
Line Input #1, file

lblcharanswer.Caption = Len(file)

Close #1


The result will only be the length of the first line.
What you would need to do is rewrite the procedure like this:

Private Sub cmdstatistics_Click()

Dim Lines as Long, Chars as Long
Dim TextLine as String

Open "C:\" & txtfilename.text For Input As #1
Do While Not EOF(1)
Line Input #1, TextLine
Lines = Lines + 1
Chars = Chars + Len(TextLine)
Loop
Close #1

lbllinesanswer.Caption = Lines
lblcharanswer.Caption = Chars

End Sub

This will count the characters and lines together in one loop

KAL_0801
05-28-2004, 02:41 PM
You can also use
Do Until EOF(1)
instead of
Do While Not EOF(1)

markp15
05-31-2004, 08:41 AM
Thank you very much for your help. I will give this a try. I have to say I am not very good at this but I am learning and its fun at the same time. However, I do get very frustrated with all the books and each one gives you a different way to do the same task. Thanks again.
Mark


The problem is that in this code:

Open "C:\" & txtfilename.text For Input As #1
Line Input #1, file

lblcharanswer.Caption = Len(file)

Close #1


The result will only be the length of the first line.
What you would need to do is rewrite the procedure like this:

Private Sub cmdstatistics_Click()

Dim Lines as Long, Chars as Long
Dim TextLine as String

Open "C:\" & txtfilename.text For Input As #1
Do While Not EOF(1)
Line Input #1, TextLine
Lines = Lines + 1
Chars = Chars + Len(TextLine)
Loop
Close #1

lbllinesanswer.Caption = Lines
lblcharanswer.Caption = Chars

End Sub

This will count the characters and lines together in one loop

Diurnal
05-31-2004, 09:32 AM
You could also check out the LOF() and FileLen() functions if you want different ways to do the same task...

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum