Manipulating External Files
Crash Neth

The commands you will use with frequency for do this is:
OPEN, LINE INPUT, GET, PUT, EOF, LOF, and the basic BASIC command.

Let's see one by one:
 
 
This is the basic command to use with external files, the usage is 
OPEN OPEN "file" FOR [INPUT, OUTPUT, RANDOM, BINARY, APPEND] AS #n 
 
where:
"file" the file that you want to open, if the file isn't at the directory that you start QB from you have to put the path.
INPUT Use this to GET data from the file
OUTPUT Use this to send data to the file
RANDOM Use this to do random-access to the file using

GET and PUT to read from a determinated RECORD (records are numbers that point to the data on the file.)
 
BINARY Use this when you want to read from any position of the file, also using GET and PUT
APPEND Use this when you want to write the data in the end of the file, the cursor is automatic positioned at the end

 
n The number the program will use when refer to the file  eg. "GET #1, a$"


 
This command is used to get a line from a previously OPENed file
LINE INPUT LINE INPUT #n, lin$
 
where:
n The number of the file you used in the OPEN command
lin$ where the line from the file is stored

When you do this command first time, he will get the first line
from the file then when you do it again he gets the second and so on.


 
This command is used with file OPENed AS BINARY or RANDOM
GET GET #n, pos, dat$
 
where:
pos For RANDOM access file is the record number and for BINARY files is the position for start reading.
dat$ Where the GETed data is stored

Let's say you OPENed the file FOR BINARY AS #1 and want to get
the data 5 bytes long at the position 10, so you do the commands:

dat$ = SPACE$(5) or dat$ = "     "   'U do this to tell QB
                                     'that you want 5 bytes
                                     'from the file.
GET #1, 10, dat$                     'And this command will
                                     'get it for you.

 


 
 
This is almost equal to GET
PUT PUT #n, pos, dat$
 
where:
pos For RANDOM files is the record number and for BINARY files is the position in the file.
dat$ Data to send to the file

To write data to files OPENed with INPUT you will use:
PRINT #n, dat$


 
End Of File
EOF EOF(n)
 
where:
n The number of the file you used in the OPEN command

Let's say you want to read all the data from "text.txt", you will do this commands:

OPEN path$ + "text.txt" for INPUT AS #1
DO WHILE NOT EOF(1)
  LINE INPUT #1, lin$
  'here you use the data
LOOP
 

 


 
Length Of File, used to get the size of a already OPENed file
LOF LOF(n)
 
where:
n The number of the file you used in the OPEN command
 

Now I will give some useful source that you can use, but please give me some credits!

* A better way to get all the data from the file:

OPEN file$ FOR INPUT AS #1
DO WHILE NOT EOF(1)                     'Here is a loop that calculate
  LINE INPUT #1, lin$                   'the number of lines in the
  lt = lt + 1                           'file
LOOP
CLOSE #1
DIM lines$(lt)                          'Here we dimension a array to
OPEN file$ FOR INPUT AS #1              'store the data
FOR i = 1 TO lt
  LINE INPUT #1, lin$
  lines$(i) = lin$
NEXT i

* A way to store an then get data in a file

'Here is the data you want to store
DATA 1,2,3,4,5
DATA 6,7,8,9,10
DATA 11,12,13,14,15
DATA 16,17,18,19,20

OPEN "datatest.dat" FOR OUTPUT AS #1
FOR i = 1 TO 4
  READ a, b, c, d, e
  lin$ = STR$(a) + "," + STR$(b) + "," + STR$(c)
  lin$ = lin$ + "," + STR$(d) + "," + STR$(e) + ","
  PRINT #1, lin$
NEXT i
CLOSE #1                                'Simple command to close the
                                        'file
OPEN "datatest.dat" FOR INPUT AS #1
DO WHILE NOT EOF(1)
  LINE INPUT #1, lin$
  FOR i = 1 TO LEN(lin$)                'Loop that goes from 1 to the
                                        'the size of the read line
    ch$ = MID$(lin$, i, 1)              'This command will get char
                                        'by char from the line
    IF ch$ = "," THEN
      fd = fd + 1
      GOTO jump
    END IF
    var$(fd) = var$(fd) + ch$
jump:
  NEXT i
  a = VAL(var$(0))                      'Here we put the data on a
  b = VAL(var$(1))                      'array
  c = VAL(var$(2))
  d = VAL(var$(3))
  e = VAL(var$(4))
  PRINT a, b, c, d, e
  FOR d = 0 TO 4                        'We need to erase the data in
    var$(d) = ""                        'the temp array to use it
  NEXT d                                'again
  fd = 0
LOOP

* Here is a program to find word in a text:

CLS
INPUT "Word to find: "; word$
INPUT "File to search: "; file$
CLS
OPEN file$ FOR INPUT AS #1
DO WHILE NOT EOF(1)
  LINE INPUT #1, lin$
  lt = lt + 1
LOOP
CLOSE #1
DIM lines$(lt)
OPEN file$ FOR INPUT AS #1
FOR i = 1 TO lt
  LINE INPUT #1, lines$(i)
NEXT i
FOR i = 1 TO lt
  FOR ii = 1 TO LEN(lines$(i)) - LEN(word$)
    test$ = MID$(lines$(i), ii, LEN(word$))
    IF test$ = word$ THEN
      SOUND 1000, .5
      CLS
      LOCATE 1, 1: PRINT "Word founded at line "; i; " at position "; ii
      LOCATE 3, 1: PRINT "The line is:"
      LOCATE 5, 1: PRINT lines$(i):
      COLOR 14
      LOCATE 5, ii: PRINT word$
      COLOR 7
      finds = finds + 1
      LOCATE 7, 1: PRINT "Any key to continue"
      SLEEP
    END IF
  NEXT ii
NEXT i
LOCATE 7, 1: PRINT "Total of words found: "; finds



 




This article originally appeared in The BASIX Fanzine Issue 17 from April 2000.