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?
Printable View
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?
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:
There is propably a better way, but that's all I could think of for the moment...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
Let me know how it works..
While that is done is there anyway to put the lines in ascending order whether or not it starts with a number or letter?
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?
Why not putting it in a array.
Then use a quicksort.
There is a sample in the threads for quicksort.
Cheers
Ray
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!
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.
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.
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:
try this one for example:Code:Dim MyVar As MyType
Dim NumOfElements As Integer
NumOfElements = FileLen("DataIn.Dat") / Len(MyVar)
Enjoy!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