|
-
Nov 21st, 2000, 12:56 PM
#1
Thread Starter
Hyperactive Member
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)
-
Nov 21st, 2000, 01:15 PM
#2
Frenzied Member
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..
-
Nov 21st, 2000, 01:25 PM
#3
Thread Starter
Hyperactive Member
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)
-
Nov 21st, 2000, 01:39 PM
#4
Frenzied Member
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?
-
Nov 21st, 2000, 02:09 PM
#5
Fanatic Member
Why not putting it in a array.
Then use a quicksort.
There is a sample in the threads for quicksort.
Cheers
Ray
-
Nov 21st, 2000, 03:30 PM
#6
Hyperactive Member
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!
-
Nov 22nd, 2000, 07:59 AM
#7
Thread Starter
Hyperactive Member
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)
-
Nov 22nd, 2000, 09:01 AM
#8
Thread Starter
Hyperactive Member
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)
-
Nov 22nd, 2000, 10:14 AM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|