Maximun number of data in an .dat or .txt files

Previous topic - Next topic

mentalthink

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
Code (glbasic) Select
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.

Slydog

I would read the file line by line using READLINE(). 
Then, DIMPUSH that line into your file contents array.
Such as (never tested!):
Code (glbasic) Select
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:
Code (glbasic) Select
FOREACH line$ in contents$[]
  DEBUG line$ + "\n"
NEXT
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

okee

Edit: slydog got there before me
Something like:

Code (glbasic) Select
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
Android: Samsung Galaxy S2 -  ZTE Blade (Orange San Francisco) - Ainol Novo 7 Aurora 2
IOS: 2 x Ipod Touch (1G)

mentalthink

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.

Kitty Hello

why not write the line count as the first line? That's how file formats do it all the time.