|
-
Apr 8th, 2007, 09:14 AM
#1
Thread Starter
Lively Member
[RESOLVED] Search Largest Number in text file
Hello
I using the Rnd Command to prodruce 13 random numbers that are stored in a text file, I need help to produce a code that would look Through the text file for the largest number
-
Apr 8th, 2007, 09:29 AM
#2
Re: Search Largest Number in text file
What is your text file looks like?
-
Apr 8th, 2007, 09:36 AM
#3
Thread Starter
Lively Member
Re: Search Largest Number in text file
well it just a 13 lines with a random number on each line.
-
Apr 8th, 2007, 09:48 AM
#4
Re: Search Largest Number in text file
In that case you can do something like this but I'm sure there is a better way:
Code:
Private Sub Command1_Click()
'============================
Dim sfile As String
Dim stext As String
Dim arLines() As String
Dim TempNumber As Single
Dim i As Integer
sfile = "c:\test.txt"
Open sfile For Input As #1
'read entire file into a string variable
'LOF is Length Of a File
stext = Input(LOF(1), #1)
Close 31
'populate array
arLines() = Split(stext, vbNewLine)
'loop through array
For i = 0 To UBound(arLines)
'check if entry is numeric
If IsNumeric(arLines(i)) Then
'check if new value is greater than previous
'CSng() is conversion function - ti converts string to single
If TempNumber < CSng(arLines(i)) Then
TempNumber = CSng(arLines(i))
End If
End If
Next i
Debug.Print TempNumber
End Sub
Last edited by RhinoBull; Apr 8th, 2007 at 10:11 AM.
-
Apr 8th, 2007, 10:06 AM
#5
Thread Starter
Lively Member
Re: Search Largest Number in text file
Thanks for the code can you explain a few things to me
I know EOF is but whats LOF
I dont know what Csng is?
-
Apr 8th, 2007, 10:12 AM
#6
Re: Search Largest Number in text file
I added comments to original post.
-
Apr 8th, 2007, 12:00 PM
#7
Re: [RESOLVED] Search Largest Number in text file
Here is another way of doing it where you read the file line by line and update the high value as needed.
Code:
Private Sub Command1_Click()
Dim strTextPath As String
Dim sngNum As Single 'holds the highest number
Dim strTemp As String 'holds the value of the current number in the file
'File with the numbers
strTextPath = "C:\numbers.txt"
'Open the file
Open strTextPath For Input As #1
'Read the lines one by one
Do While Not EOF(1)
Line Input #1, strTemp
'Check if the current number is larger than the previous high number
If CSng(strTemp) > sngNum Then
'Update the high number if the current number is larger
sngNum = CSng(strTemp)
End If
Loop
Close #1
MsgBox sngNum
End Sub
-
Apr 8th, 2007, 10:04 PM
#8
Thread Starter
Lively Member
Re: [RESOLVED] Search Largest Number in text file
Thank you for your this code, can can anybody help me find the lowest number?
-
Apr 9th, 2007, 01:02 AM
#9
Re: [RESOLVED] Search Largest Number in text file
use the above function and just change the > to < like this...
vb Code:
If CSng(strTemp) < sngNum Then
this should help you....
If an answer to your question has been helpful, then please, Rate it!
Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.
-
Apr 9th, 2007, 05:44 AM
#10
Re: [RESOLVED] Search Largest Number in text file
Like ganeshmoorthy said use the code above but test for less than. You must also initialize a low number. In this example I'm taking the first number and using it to initialize sngNum.
Code:
Private Sub Command1_Click()
Dim strTextPath As String
Dim sngNum As Single 'holds the highest number
Dim strTemp As String 'holds the value of the current number in the file
Dim blnRunning As Boolean
'File with the numbers
strTextPath = "C:\numbers.txt"
'Open the file
Open strTextPath For Input As #1
'Read the lines one by one
Do While Not EOF(1)
Line Input #1, strTemp
'Check if the current number is larger than the previous high number
'Initialize a starting low number
If Not blnRunning Then
sngNum = CSng(strTemp)
bnlrunning = True
End If
If CSng(strTemp) < sngNum Then
'Update the low number if the current number is lower
sngNum = CSng(strTemp)
End If
Loop
Close #1
MsgBox sngNum
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|