Sharp OZ-707 Operation Manual - Page 35

Program 1, Creating a Sequential File, Reading Data from a Sequential File, Program 2, Accessing

Page 35 highlights

Program 1 - Creating a Sequential File 10: DIM DE$(1) 20: OPEN "E:DATA" FOR OUTPUT AS #20 30: CLS 40: INPUT "NAME: ";NA$ 50: IF NA$ = "DONE" THEN 100 60: INPUT "DEPARTMENT: ";DE$(1) 70: INPUT "DATE HIRED: ";HI$ 80: PRINT #20,NA$;",";DE$(1);",";HI$ 90: GOTO 30 100: CLOSE #20 110: END Before execution, enter: INIT "E:10K" IENTER I to allocate storage space in the RAM disk E. RUN NAME: SAMUEL GOLDWYN DEPARTMENT: AUDIONISUAL AIDS DATE HIRED: 01/12172 NAME: MARVIN HARRIS DEPARTMENT: RESEARCH DATE HIRED: 12103/65 NAME: DEXTER HORTON DEPARTMENT: ACCOUNTING DATE HIRED: 04/27/81 NAME: DONE Reading Data from a Sequential File . Now look at Program 2. It accesses the file DATA that was created In Program 1 and displays the name of everyone hired in 1981. Program 2 - Accessing a Sequential File 10: DIM DE$(1) 20: OPEN "E:DATA" FOR INPUT AS #20 30: INPUT #20,NA$,DE$(1 ),HI$ 40: IF RIGHT$(HI$,2)="81" THEN PRINT NA$ 50: GOTO 30 58 RUN DEXTER HORTON Input past end in 30 Program 2 reads, sequentially, every item in the file, and prints the names of employees hired in 1981. When all the data has been read, line 30 causes an ERROR. To avoid this error, use the EOF function, which tests for the end-of-file. The revised program looks like this: 10: DIM DE$(1) 20: OPEN "E:DATA" FOR INPUT AS #21 25: IF EOF(21) THEN 60 30: INPUT #21 ,NA$,DE$(1 ),HI$ 40: IF RIGHT$(HI$,2)="81" THEN PRINT NA$ 50: GOTO 25 60: CLOSE #21 70: END As shown by these programs, the following steps are required to create a sequential file and access the data in it: 1. OPEN the file for OUTPUT. 2. Write data to the file using the PRINT# statement. 3. CLOSE the file and reopen it in INPUT mode to read the data. 4. Use the INPUT# statement to read data from the file into the program. Adding Data to a Sequential File If you have an existing data file and want to add more data to the end of it, you cannot simply open the file in the OUTPUT mode and start writing data. As soon as you open a sequential file in the OUTPUT mode, you destroy its current contents. Instead, use the APPEND mode. If the file does not already exist, the OPEN statement will work exactly as it would if the OUTPUT mode had been specified. The following procedure can be used to add data to an existing file called "FOLKS". 59

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128

Program 1 -
Creating a Sequential File
10:
DIM
DE$(1)
20: OPEN "E:DATA" FOR OUTPUT AS #20
30: CLS
40:
INPUT "NAME: ";NA$
50: IF NA$
=
"DONE" THEN 100
60: INPUT "DEPARTMENT: ";DE$(1)
70: INPUT "DATE HIRED: ";HI$
80: PRINT #20,NA$;",";DE$(1);",";HI$
90: GOTO 30
100: CLOSE #20
110: END
Before execution, enter: INIT "E:10K"
I
ENTER
I
to
allocate storage space
in the
RAM
disk
E.
RUN
NAME: SAMUEL GOLDWYN
DEPARTMENT: AUDIONISUAL AIDS
DATE HIRED:
01/12172
NAME: MARVIN HARRIS
DEPARTMENT: RESEARCH
DATE HIRED:
12103/65
NAME: DEXTER HORTON
DEPARTMENT: ACCOUNTING
DATE HIRED:
04/27/81
NAME: DONE
Reading Data from a Sequential File
.
Now look at Program 2. It accesses the file DATA that was created
In
Program 1 and displays the name of everyone hired in 1981.
Program 2 -
Accessing a Sequential File
10:
DIM
DE$(1)
20: OPEN "E:DATA" FOR INPUT AS #20
30: INPUT #20,NA$,DE$(1 ),HI$
40:
IF
RIGHT$(HI$,2)="81" THEN PRINT NA$
50: GOTO 30
58
RUN
DEXTER HORTON
Input past end
in
30
Program 2 reads, sequentially, every item in the file, and prints the
names of employees hired in 1981. When all the data has been read,
line 30 causes
an
ERROR. To avoid this error, use the EOF function,
which tests for the end-of-file. The revised program looks like this:
10: DIM DE$(1)
20: OPEN "E:DATA" FOR INPUT AS
#21
25: IF EOF(21) THEN 60
30: INPUT
#21
,NA$,DE$(1 ),HI$
40:
IF
RIGHT$(HI$,2)="81" THEN PRINT NA$
50: GOTO 25
60: CLOSE
#21
70: END
As shown
by
these programs, the following steps are required to create
a sequential file and access the data
in
it:
1.
OPEN the file for OUTPUT.
2.
Write data to the file using the PRINT# statement.
3.
CLOSE the file and reopen it
in
INPUT mode to read the data.
4.
Use the INPUT# statement to read data from the file into the
program.
Adding Data to a Sequential File
If you have
an
existing data file and want to add more data to the end
of it, you cannot simply open the file
in
the OUTPUT mode and start
writing data.
As
soon as you open a sequential file in the OUTPUT
mode, you destroy its current contents.
Instead, use the APPEND mode.
If
the file does not already exist, the
OPEN statement will work exactly
as
it would
if
the OUTPUT mode
had been specified.
The following procedure can be used to add data to
an
existing file
called "FOLKS".
59