April 18, 2024, 06:37:42 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Store file to string array

Started by aurelCB, October 09, 2009, 01:26:01 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

aurelCB

October 09, 2009, 01:26:01 PM Last Edit: October 09, 2009, 01:27:49 PM by aurelCB
Hi all...
Tom give me idea how simply read file(txt).
And i make one simple program which read small txt file line by line
and store each line as one element of string array.
First we must make this txt file.I use notpad for this:
1.First line
2.Line 2
3.Line 3
4.Line 4
5.Line 5
6.Line 6
7.Line 7
8.Line 8
9.Line 9
10.Line 10


So then just save file as TestToArray.txt
After that load program which read this file:


'Read file line by line and store lines in string array
DEF myfile:FILE
'string array with 20 elements
DEF ln[20]:STRING
'storing array
DEF source[20]:STRING
DEF x,linenum:int

OPENCONSOLE
IF(OPENFILE(myfile,getstartpath + "TestToArray.txt","R") = 0)
'--------------------------------------------------------
Do :' loop
IF(READ(myfile,ln[x]) = 0)
'count lines
linenum=linenum+1
            PRINT ln[x]
'store line in new array element
source[linenum]=ln[x]
'Show line by line
ENDIF
Until EOF(myfile)
'--------------------------------------------------------
CLOSEFILE myfile
'show how many lines have in file
Print "Number of lines is :",linenum
Print
Print source[5]:' show fifth element
Print

   PRINT "File read successfully!"
ELSE
   PRINT "File not found!"
ENDIF

PRINT "Press Any Key To Close"
DO:UNTIL INKEY$ <> ""
CLOSECONSOLE
END


I think that will be usefull. :)
Aurel