Results 1 to 10 of 10

Thread: [RESOLVED] Search Largest Number in text file

  1. #1

    Thread Starter
    Lively Member VBN00b001's Avatar
    Join Date
    Feb 2007
    Posts
    68

    Resolved [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

  2. #2

  3. #3

    Thread Starter
    Lively Member VBN00b001's Avatar
    Join Date
    Feb 2007
    Posts
    68

    Re: Search Largest Number in text file

    well it just a 13 lines with a random number on each line.

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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

  5. #5

    Thread Starter
    Lively Member VBN00b001's Avatar
    Join Date
    Feb 2007
    Posts
    68

    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?

  6. #6

  7. #7
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    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

  8. #8

    Thread Starter
    Lively Member VBN00b001's Avatar
    Join Date
    Feb 2007
    Posts
    68

    Re: [RESOLVED] Search Largest Number in text file

    Thank you for your this code, can can anybody help me find the lowest number?

  9. #9
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    Re: [RESOLVED] Search Largest Number in text file

    use the above function and just change the > to < like this...
    vb Code:
    1. 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.


  10. #10
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    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
  •  



Click Here to Expand Forum to Full Width