Hi I like to know how kwno the number of data in a .txt or .dat file.
The file can be have inside 3 lines or 1000 or 254 but how I know this value, how I make the counter if I don´t know the end of the entries in the file?¿.
i.e.
I have a .txt , it´s have this 3 lines
320.4
10.2
11.2
Well... how I now that´s file have 3 entries for make a redim, and load this info into an array.
Thanks in advance.
Iván J.
I would read the file line by line using READLINE().
Then, DIMPUSH that line into your file contents array.
Such as (never tested!):
LOCAL contents$[]
LOCAL line$
LOCAL fh# // File Handle
DIM contents$[0] // Clear contents
fh = GENFILE()
OPENFILE(fh, "file.name", 1)
WHILE NOT ENDOFFILE(fh)
READLINE fh, line$
DIMPUSH contents$[], line$
WEND
CLOSEFILE fh
Now, the contents$[] array contains your entire file, with each array location being a separate line.
So, you can now use a FOREACH statement to step through each line, such as the following to dump the file to the debug window:
FOREACH line$ in contents$[]
DEBUG line$ + "\n"
NEXT
Edit: slydog got there before me
Something like:
IF OPENFILE (1,"data.txt",1)
GLOBAL NoOfLines = 0
LOCAL TheLine$
WHILE NOT ENDOFFILE(1)
READLINE 1, TheLine$
NoOfLines = NoOfLines + 1
WEND
PRINT "The textfile has " + NoOfLines,0,0
ELSE
PRINT "couldn't open file",0,10
ENDIF
SHOWSCREEN
KEYWAIT
will find the number of lines
but you can also use endoffile() to DIMPUSH the data into an array
Thanks slydog and okee, I don´t use never the endofline command, and I don´t look to now, I think GLBasic don´t have this command.
Thanks again guys, this goes to helpme a lot of.
Kinds Regards,
Iván J.
why not write the line count as the first line? That's how file formats do it all the time.