The following demo takes a single text file as input and reads it into a single string variable. The work is done by the bget# function which reads a single byte from the file and adds it to the string.
My test file used for input. We shall read this in byte by byte (carriage returns and all) |
The program running. Here we decided to do something useful with the data, ie split it up into individual records. |
REM reading an entire file into a string
REM and outputing one line at a time
REM T Street
REM 2015-03-18
INSTALL @lib$+"stringlib" : REM used by the split function
fulltext$ = "" : REM string to hold all the input
CRLF$ = CHR$(13)+CHR$(10) : REM carriage return line feed
DIM eachline$(1) : REM array to store each line of the input
REM I don't know how big this should be yet
REM read the input file into the string
REM -----------------------------------
REM open file for reading
infile% = OPENIN("textfile.txt")
WHILE NOT(EOF#infile%)
REM read each byte from the text file
REM and convert to ASCII character
REM append to the text string
fulltext$ += CHR$(BGET#infile%)
ENDWHILE
REM now we have the full text file in a single string
PRINT "The full text is"
PRINT "----------------"
PRINT fulltext$
PRINT "----------------"
REM split the textfile at the carriage returns
REM to create an array
REM we can then do what we like with the array
parts% = FN_split( fulltext$, CRLF$, eachline$() )
PRINT "There are "STR$(parts% DIV 3)" record(s) in the file"
n% = 0
WHILE n% < parts%
PRINT "-------------------------"
PRINT "RECORD #"STR$(n% DIV 3+1)
PRINT "Dinoaur : ", eachline$(n%)
n% += 1
PRINT "name means '" eachline$(n%) "'"
n% += 1
PRINT "length : " eachline$(n%)
n% += 1
PRINT
ENDWHILE
REM and outputing one line at a time
REM T Street
REM 2015-03-18
INSTALL @lib$+"stringlib" : REM used by the split function
fulltext$ = "" : REM string to hold all the input
CRLF$ = CHR$(13)+CHR$(10) : REM carriage return line feed
DIM eachline$(1) : REM array to store each line of the input
REM I don't know how big this should be yet
REM read the input file into the string
REM -----------------------------------
REM open file for reading
infile% = OPENIN("textfile.txt")
WHILE NOT(EOF#infile%)
REM read each byte from the text file
REM and convert to ASCII character
REM append to the text string
fulltext$ += CHR$(BGET#infile%)
ENDWHILE
REM now we have the full text file in a single string
PRINT "The full text is"
PRINT "----------------"
PRINT fulltext$
PRINT "----------------"
REM split the textfile at the carriage returns
REM to create an array
REM we can then do what we like with the array
parts% = FN_split( fulltext$, CRLF$, eachline$() )
PRINT "There are "STR$(parts% DIV 3)" record(s) in the file"
n% = 0
WHILE n% < parts%
PRINT "-------------------------"
PRINT "RECORD #"STR$(n% DIV 3+1)
PRINT "Dinoaur : ", eachline$(n%)
n% += 1
PRINT "name means '" eachline$(n%) "'"
n% += 1
PRINT "length : " eachline$(n%)
n% += 1
ENDWHILE
Diplodocus - spending his days one bite to the next. |