Results 1 to 9 of 9

Thread: EOF Question

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344
    If there is a file called Datain.dat, is there anway to get how many lines are written in that dat file using the EOF function?
    -RaY
    VB .Net 2010 (Ultimate)

  2. #2
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    You could open it and loop through it in Line Input mode and each time you loop through it, you could increment a counter, until you hit the EOF.

    Example:

    Code:
    Private Sub Command1_Click()
        Dim strFile As String
        Dim sText As String
        Dim lngCnt As Long
        
        strFile = "c:\my documents\Datain.dat"
        
        Open strFile For Input As #1
        
        Do Until EOF(1)
            Line Input #1, sText
            lngCnt = lngCnt + 1
        Loop
        
        MsgBox lngCnt
        
    End Sub
    There is propably a better way, but that's all I could think of for the moment...

    Let me know how it works..


  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344

    And

    While that is done is there anyway to put the lines in ascending order whether or not it starts with a number or letter?
    -RaY
    VB .Net 2010 (Ultimate)

  4. #4
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    I suppose there are probably numerous ways at accomplishing that.

    Basically the idea would be to read in the text file line by line, storing it somewhere, sorting it, and then outputting it to the original file.

    The storing it somewhere and sorting it part is the big question.. You could store it into a database file and then run a query on it to bring back the records sorted and then output that to a text file..

    Basically, I don't believe there is any direct way of sorting the text file, you must store it, sort it and then output it back to a text file..Unless someone else knows of a better way?

  5. #5
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Worldwide in the Sun
    Posts
    566
    Why not putting it in a array.
    Then use a quicksort.
    There is a sample in the threads for quicksort.

    Cheers
    Ray
    Ray

  6. #6
    Hyperactive Member marnitzg's Avatar
    Join Date
    Oct 2000
    Location
    South Africa
    Posts
    372
    If you don't want to waste memory, you can scan through the file finding positions. ie. scan through the file storing line numbers in an array. So put line number 1 in position 1. Scan line 2. If line 2 is smaller then put it in position 1 and move pos 1 to pos 2 etc. Therefore you'll have the line numbers in an array. You could also just search for the smallest, write it off, then search for the second etc. This does take long but hardly uses any memory, rather than storing the entire contents of the text file in memory!

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344

    Hey thanks

    Thanks a lot, yeah I would check the array and quick sort. The array rout is the one I need to go, thats what I was looking into to better use array's and loops. Thanks a lot, I'll let you know how it goes.
    -RaY
    VB .Net 2010 (Ultimate)

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344

    ok lets say

    lets say I set a dimensional array, one that is not static.... I need to count the lines in the file first so I can tell my array how many blocks it should be.....since each line is going into a seperate block. Any idea how I can attack that? I understand the array part pretty well its ripping each line and putting it in the array i have the problem with. Especially if i don't know how many lines the file is going to be.
    -RaY
    VB .Net 2010 (Ultimate)

  9. #9
    Guest
    If you're using a binary datfile you're most probably using an UDT (User Defined Type). If that is the case then this should work:

    Code:
        Dim MyVar As MyType
        Dim NumOfElements As Integer
        
        NumOfElements = FileLen("DataIn.Dat") / Len(MyVar)
    try this one for example:

    Code:
    Private Type MyType
        Name As String * 50 ' 50 Bytes
        Code As Integer     ' 2 Bytes
        '...
        'Etc
    End Type
    
    Private Sub Form_Load()
        Dim MyVar As MyType
        Dim NumOfElements As Integer
        
        NumOfElements = FileLen("DataIn.Dat") / Len(MyVar)
    End Sub
    Enjoy!

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