NOS : Certificate in Computer Applications
| Home | Table of Contents |
LESSON 28
PROCESSING FILES IN BASIC
28.1 INTRODUCTION
We have so far discussed programs where data is supplied either through READ...DATA statements or through INPUT statements. But it is difficult to handle large volume of data with the help of these statements. Moreover, data stored in one program cannot be shared by other programs. The above limitations can be overcome by placing the data into a separate storage area called a data file.
A date file forms an essential framework of a data processing system. It is a collection of data organised in a specific format and for a specific purpose, which is kept somewhere in external memory. A file can be described as a collection of records where a record consists of number of data values, which are either produced by a computer as per the instructions of a program or given to the computer as a data. A typical data processing system many consist of many files. Many records in a file contain detailed information about some aspect of the system. For example, a "pay file" may contain one record for each employee containing all the particulars such as Employee Code, Name, Address, Basic pay, HRA, DA, CCA, Designation, Sex, etc. Each partition is a data item and is referred to as a field. A number of BASIC Programs can use this file as input.
File handling methods differ widely from system to system. In this lesson, we will discuss the methods that are currently used in most of the IBM compatible systems.
28.2 OBJECTIVES
On completion of this lesson, you should be able to:
28.3 TYPES OF FILES
Depending on the way in which the data is stored and accessed, files are categorised as:
Sequential Access Files
These files may be created on a magnetic tape or disk. Data from these files is read sequentially, item after item, starting at the beginning. Though sequential access file is easy to handle, yet it has a serious disadvantage. In the sequential file we cannot change the existing entry or insert a new entry. To do this, we should copy the entire content to another file making the changes on the way and then copy the correct data back in to the original file. Another disadvantage is that we cannot simply locate or go back to read a particular data item.
Fig. 28.1 and 28.2 show how data is stored on magnetic tapes and disk respectively. A disk consists of a number of concentric circles known as tracks. The data items are stored on these tracks sequentially.
ADD PIC
Fig. 28.1 Sequential File on a Tape
Consider an inventory file shown in Table 28.1
| Part name | Part number | Price | Quantity |
| AAAA | 101 | 8.00 | 100 |
| BBBB | 102 | 10.00 | 200 |
| CCCC | 103 | 12.00 | 300 |
Table 28.1 Inventory file
ADD PIC
Fig. 28.2 Sequential File on a Disk
Details of each part are stored in one record. When we complete all the parts, the data file would look like a long column of values as shown below:
AAAA
101
8.00
100
BBBB
102
10.00
200
CCCC
103
12.00
300
Programs are written to read these data values one after another.
Random Access Files
Random access files are created on disks to allow us to read or write from the files in random order. Random access files allow more flexible access than the sequential files. You can read or write any record directly in a random file without searching through all the records that precede it. Thus, reading and writing of data is faster.
28.4 HANDLING OF SEQUENTIAL FILES
Using data files would require the incorporation of statements in your program to perform the following functions:
28.4.1 Naming a file
A file is identified by its file specifications. It is of the form:
Device name: File name
The device name tells the system which device to look for the file and the file name tells which file to look for on that device. The device name should be followed by a colon as indicated. Examples are:
A: MEAN
B: STUDENT. MKS
The device names A and B indicate the disk drive one and two respectively
File names can have a maximum of eight characters. However, the files stored on disks may have a file name extension in the following form.
NAME.EXT
The extension consists of a period and followed by three letters. When you use longer file name, one of the following conditions may occur.
File Name |
Result |
|
| 1. | Extension is longer than three characters. | Extra characters are truncated. |
| 2. | Name is longer than eight characters. | A period is inserted after the eighth character and extra characters (up to three) are used as the extension. |
| 3. | Name is longer than eight characters and extension is included. | Gives an error. |
For example,
SURENDERKUMAR becomes SURENDER.KUM.
PROGRAM.PENSION becomes PROGRAM.PEN.
PAYROLLCALUCTION.BAS gives an error.
28.4.2 Opening a file
It is necessary to establish a line of communication between a program and a file that is to be used before data can be read from or written to that file. This is achieved by using the OPEN command as shown below:
OPEN ["Mode", #file number, "file name"]
Mode is a string constant and takes one of the following three forms
It should be noted that the mode is enclosed in quotation marks.
File number is an integer number and can be any number between 1 and 15. It is a unique number associated with the actual physical file. A file number may be a number, variable, or expression.
File name specifies the particular file to be used. This should conform to the specifications of the file when it was created. File names must be enclosed in quotations.
Examples of opening sequential files for input and output operations are:
10 OPEN "I" # 1, " PANKAJ.DTA"
20 OPEN "O", # 3, "TEMP"
These can also be written as
10 OPEN" PANKAJ.DTA" FOR INPUT AS# 1
20 OPEN" TEMP" FOR OUTPUT AS #3
If a file opened for input operation does not exist, a "FILE NOT FOUND" error will be shown on the screen. If a file, which does not exist, is opened, in that case file will be created. It is important to note that opening a file for output destroys the existing data in that file.
28.4.3 Closing a File
A previously opened file can be closed using the CLOSE statement as follows
CLOSE [# file number, #file number, ......]
The file number is the number used in the OPEN statement. For example, the statement.
100 CLOSE #1, # 3
causes the files with file numbers 1 and 3 to be closed. The CLOSE statement is used when you have finished processing data from a file.
A CLOSE statement with no file numbers specified causes all files that have been opened to be closed.
You should also note that execution of END and RUN statements also causes all open sequential files to be automatically closed. However, STOP does not close any files.
28.4.4 Writing Data to a File
We may write data to a sequential file using either of the following two statements.
PRINT [# file number, list of variables]
WRITE [ #file number, list of variables ]
File number refers to the file designator used when the file was opened for output. List of variables specifies the data that is to be written to the file.
The difference between PRINT # and WRITE # Statements is that WRITE # inserts commas between the data items and quotation marks for strings while PRINT # causes data to be written without any delimiters (commas, etc.). For example consider the statement
40 PRINT #1 N $, AMT
In case N $ = " PANKAJ" and AMT = 2000, the
statement would write the following image to the file 1:
PANKAJ 2000
The statement
40 WRITE # 1, N $, AMT
would write the following image to the file 1:
"PANKAJ", 2000
The delimiters are useful when we try to read the data with an INPUT # statement.
Let us take an example for proper understanding the use of "WRITE" and "PRINT" statement.
Example 1
Program
10 REM *** PROGRAM INVENTI ***
20 REM -- PNM$ PART NAME
30 REM -- PN PART NUMBER
40 REM -- P PRICE
50 REM -- QTY QUANTITY
60 OPEN "O", # 1, "INVENT. DTA"
70 FOR I =1 TO 3
80 INPUT "NAME" ; PNM$
90 INPUT "NUMBER" PN
100 INPUT "PRICE" P
110 INPUT "QUANTITY" ; QTY
120 WRITE # 1, PNM$, PN, P, QTY
130 PRINT
140 NEXT I
150 CLOSE #1
160 END
Output
NAME? AAAA
NUMBER? 101
PRICE? 8
QUANTITY? 100
NAME? BBBB
NUMBER? 102
PRICE? 10
QUANTITY? 200
NAME? CCCC
NUMBER? 103
PRICE? 12
QUANTITY? 300
28.4.5 Reading Data from a File
Data can be read from a sequential file using the INPUT # statement. It takes the form
INPUT [ # file number, list of variables ]
The following INPUT # statement can be used to read data from the file INVENT. DTA:
200 INPUT #1, PNM$, PN, P, QTY
The type of data in the file must match the type specified in the list. Unlike INPUT, no question mark is displayed with INPUT #
The following example will illustrate clearly the use of INPUT # statement.
Example 2
Write a program to read inventory data stored in the file INVENT. DTA inventory and print the table with the value of each product.
Program
10 REM ** PROGRAM INVENT 2 **
20 OPEN I",#1, "INVENT. DTA"
25 PRINT "PART", "PART", " PRICE", "QUANTITY", " VALUE "
30 PRINT "NAME","NUMBER"
40 PRINT
50 INPUT #1 PNM$, PN, P, QTY
60 LET V= P* QTY
70 PRINT PNM$, PN, P, QTY, V
80 GOTO 50
90 CLOSE
100 END
Output
PART NAME |
PART NUMBER |
PRICE |
QUANTITY |
VALUE |
AAAA |
101 |
8 |
100 |
800 |
BBBB |
102 |
10 |
200 |
2000 |
CCCC |
103 |
12 |
300 |
3600 |
Input past end in 50
The program in Example 2 reads sequentially, every item in the file. When all the data have been read, line 50 causes an "Input past end" error. This can be avoided by using the EOF function. This function tests for end of file and the control is directed appropriately. The end of the file is indicated by a special character in the file.
The program INVENT2 is modified as follows:
10 REM ** PROGRAM INVENT 2**
20 OPEN "I", #1 "INVENT. DTA"
25 PRINT "PART", "PART","PRICE, "QUANTITY" "VALUE"
30 PRINT "NAME", "NUMBER"
40 PRINT
50 IF EOF (1) THEN 100
60 INPUT #1, PNM$, PN, P, QTY
70 LET V = P*QTY
80 PRINT PNM$, PN, P, QTY, V
90 GOTO 50
100 CLOSE
110 END
28.4.6 Reading lines from a File
The LINE INPUT # statement may be used to read an entire line, ignoring delimiters, from a sequential file and assign to a string variable. It is of the form
LINE INPUT [ #file number, string variable ]
File number is the number under which the file was opened and string variable is used to store a line of string constant.
LINE INPUT # reads all characters in the sequential file up to a carriage return which was inserted by the PRINT # statement. The next LINE INPUT # reads all characters up to the next carriage return. Example 3 illustrates the use of LINE INPUT #statement.
Example 3
Write a program to input employee addresses from the keyboard, store them in a file named ADDRESS, and read back to produce a list.
Program
10 REM ** PROGRAM EMPLOYEE **
20 REM ** INPUT EMPLOYEE ADDRESS THROUGH KEYBOARD **
30 OPEN "O ", #1 "ADDRESS"
40 FOR I = 1TO 3
50 LINE INPUT "TYPE IN ADDRESS" ADDR$
60 PRINT # 1 ADDR$
70 NEXT I
80 CLOSE 1
85 PRINT
90 OPEN : "I" # 1, "ADDRRESS"
100 FOR I= 1 TO 3
110 LINE INPUT # 1, ADDR$
120 PRINT ADDR$
130 NEXT I
140 CLOSE 1
150 END
Output
TYPE IN ADDRESSES? PANKAJ KUMAR GOEL, PITAMPURA, DELHI -34
TYPE IN ADDRESSES? RAJESH KUMAR, AUDIT OFFICE, GWALIOR
TYPE IN ADDRESSES? DR. SANJU, MEDICAL COLLEGE, JODHPUR
PANKAJ KUMAR GOEL, PITAMPURA, DELHI-34
RAJESH KUMAR, AUDIT OFFICE, GWALIOR
DR. SANJU, MEDICAL COLLEGE, JODHPUR
28.4.7 Adding Data to a Sequential File
To add any data to a sequential file that is already residing on a disk, you cannot just open the file and write data to it. You must note that when a sequential file is opened for output, its current contents are lost. In such cases, you should open the file with APPEND. For example, the statements
10 FILE = " A: TEMP"
20 OPEN FILE FOR APPEND AS #1
will open the file named TEMP on the disk in drive A and position the file pointers at the existing data. Now we can add records to the file, which has been numbered as 1.
Following example will illustrate the use a of Append
Example 4
Write a program to add two more products to the file "INVENT. DTA" created earlier in Example 1.
DDDD 104 20.0 200
EEEE 105 30.0 300
Program
10 FILES = "INVENT. DTA"
20 OPEN FILES FOR APPEND AS #1
30 FOR I=1 TO 2
40 INPUT PNM$, PN, P, QTY
50 WRITE #1, PNM$, PN, P, QTY
60 NEXT I
70 CLOSE # 1
80 REM ** DATA INPUT FROM FILES **
90 OPEN "I", #1, "INVENT. DTA
100 PRINT
110 IF EOF (1) THEN 160
120 INPUT #1, PNM$, PN, P, QTY
130 PRINT PNM$, PN, P, QTY
140 GOTO 110
150 CLOSE # 1
160 END
Output
? DDDD, 104, 20, 200
? EEEE, 105, 30, 300
AAAA 101 8 100
BBBB 102 10 200
CCCC 103 12 300
DDDD 104 20 200
EEEE 105 30 300
28.5 HANDLING OF RANDOM ACCESS FILES
Unlike sequential files, random files cannot be created so easily. Random files require more programming steps and skills. However, since random access files effectively retrieve or write individual data items anywhere in the file they are often preferred.
28.5.1 Creating a Random File
Following are the steps required to create a random file:
Step1: Like sequential files, we may open random files using the OPEN Statement
OPEN ["R", #File number,"File name",Record length]
The character R indicates that the file to be opened is a random access file. Record length is a number that specifies the length of records. The length may range from 1 to 32767, The default record length is 128 bytes. For examples,
100 OPEN "R", #2, "PANKAJ',100
200 OPEN "R",#3, "CHAP5",120
These statements can also be written as
100 OPEN "PANKAJ" AS#2LEN =100
200 OPEN "CHAPS," AS #3 LEN = 120
Step2: Space in the random buffer is allocated by the FIELD statement in the form
FIELD [#file number, w1 AS u1, w2 AS u2...]
File number is the number of the concerned file just opened, w1 specifies the number of character positions to be allocated to the string variable and so on.
A FIELD statement defines variables that are used to put data into or get data out of a random buffer. For example, the statement
50 FIELD #2, 20 AS A$ 10 AS B$
defines two string variables A$ and B$ and allocates first 20 bytes to the variable A$ and next bytes to B$. Note that FIELD is a declaration statement and does not actually place data into the random buffer.
Unlike sequential files, random files cannot be created so easily. Random files require more programming steps and skills. However, since random access files effectively retrieve or write individual data items anywhere in the life, they are often preferred.
The total number of bytes allocated in a FIELD statement must not exceed the record length specified in the OPEN statement. For instance, the statements
10 OPEN "R",#2 "CHAP1",40
20 FIELD #2,20 AS A$, 5 AS B$, 10 AS C$, 10 AS D$
open a file named CHAP1 as a random file and allocates character positions as follows:
First 20 bytes to A$
Next 5 bytes to B$
Next 10 bytes to C$
Next 10 bytes to D$
Note that the total number of bytes allocated is 45, which is more than 40, the record length as defined in the OPEN statement in line 10. In this case, a "Field overflow" error will appear on the screen.
Step 3: Placing data into the random file buffer is done by the LSET and RSET statements. They take the form:
LSET u$ = x $
RSET u$ = x $
Where u$ is the name of a string variable that is defined in a FIELD statement and x$ is a string variable representing the data to be placed into the field identified by u$ Examples of these statements are:
30 LSET A$ = F1$
40 RSET B$ = F2$
LSET statement left-justifies the string in the field, and RSET statement right justifies the string. Let us suppose F1$ represents CONNECTING-PROD and F2$ represents 5000. If the character positions allocated to A$ is 18 andB$ is 5, then the field represented by A$ and B$ in the random buffer are filled like this:
| C | O | N | N | E | C | T | I | N | G | - | P | R | O | D | 5 | 0 | 0 | 0 |
|
------------------------A$----------------------------------------
|
---------------
|
Since all the fields in a random buffer are defined as string fields, any number value that is to be placed in a random file buffer must be converted to string value before it is LSET or RSET. This can be done using the following `convert' functions.
MKI$ converts an integer to a 2-byte string.
MKS$ converts a single-precision number to a 4-byte string.
MKD$ converts a double-precision number to an 8-byte string.
LSET A$ MKI$ ( integer variable)
or
RSET A$ MKD$ (double-precision variable)
Where A$ is the string variable defined in the FIELD statement.
For example
LSET A$ - MKI$(RN)
RSET B$- MKS$ (PAY)
Step 4: Moving of data from a random buffer to a random file is done by the PUT statement.
Each time a PUT statement is executed, a record is written to the file.
PUT {# file number, record number]
File number refers to the number under which the file was opened. Record number is the number of the record to be written and expressed as an integer constant or variable. If record number is omitted, the record will have the next available number. For example, the statement
100 PUT #2, 1
will write the information contained in the buffer to the first record of the random file numbered 2. It can also be written as
100 PUT #2,K
where K is an integer variable. The value of K must be defined before using the PUT statement.
Each time a PUT statement is executed, a record is written to the file.
Let us take an example to clarify various statements discussed for creating a random file.
Example 5
Write a program to open a random and record salary file statement of employees with the following details:
1 Name
2 Basic Pay
3 D.A.
4 H.R.A.
5 C.C.A.
Assume the following data:
Name |
Basic Pay |
D.A. |
H.R.A. |
C.C.A. |
UPMA |
25,000 |
5000 |
8000 |
2000 |
SANJU |
20,000 |
4000 |
7000 |
1500 |
ANAMIKA |
15,000 |
3000 |
6000 |
1000 |
Program
10 REM ** CREATION OF RANDOM FILE **
20 OPEN " R",#1,"SALARY" 60
30 FIELD #1,20 AS N$, 10 AS B$ 10 AS D$, 10 AS H$, 10 AS C$
40 INPUT "RECORD NUMBER"; I
50 IF I=0 THEN 150
60 INPUT "NAME "; A$
70 INPUT "BASIC PAY"; P
80 INPUT " DEARNESS ALLOWANCE"; Q
82 INPUT "HOUSE RENT ALLOWANCE"; R
85 INPUT " COMPENSATORY CITY ALLOWANCE"; S
100 LSET N$ = A$
110 LSET B$ = MKS$ (P)
120 LSET D$ = MKS$ (Q)
122 LSET H$= MKS$ (R)
125 LSET C$ = MKS$ (S)
130 PUT # 1, I
140 GOTO 40
150 CLOSE # 1
160 END
Output
Record Number? 1
Name? UPMA
Basic pay? 25000
Dearness Allowance? 5000
House Rent Allowance? 8000
Compensatory city Allowance ? 2000
Record Number? 10
Name? SANJU
Basic pay? 20000
DA? 4000
HRA 7000
CCA? 1500
RECORD NUMBER? 40
Name? Anamika
Basic Day? 15000
DA? 3000
HRA? 6000
CCA? 1000
Record Number 0
28.5.2 Accessing a Random File
Like creation, accessing a random file includes the following points:
Step 1: Follow the procedure of step 1 in creating a file. (see Section 5.4.1)
Step 2: Follow the procedure of step 2 in creating a file. (see section 5.4.1)
Step 3: Bringing data from the file to the buffer is done by the GET statement. This is similar to the PUT statement.
GET [#file number, record, number]
Record number is optional and, if it is omitted, the next record (after the last GET ) is read into the buffer. For example,
200 GET #2, 10
will transfer the content of the record number 10 from the file number 2 to the buffer.
Step 4: After a GET statement, the data available in the buffer can be used by the program for further manipulation and printing outputs. Remember that the numeric values are recorded as string values in the buffer and therefore all numeric values must be converted back to numbers for performing any arithmetic operations on them: This is done using following "convert" functions:
CVI converts a two-byte string to an integer.
CVS converts a 4-byte string to a single-precision number.
CVD converrts an 8-byte string to a double-precision number.
These functions are written in the form:
x = CVI (u$)
x = CVS (u$)
x = CVD (u$)
u$ is the name of a string variable containing numeric values in the buffer and x is a numeric variable representing the data to be used in the program.
The following program segment illustrates the use of GET and convert functions:
40 FIELD #2, 10 AS A$, 6 AS B$, 3 AS C$
50 GET #2
55 LET N$ = A$
60 LET M1 = CVS (B$)
70 LET R1 = CVI (C$)
80 PRINT N$, M1, R1
This program uses a random file numbered 2, which has fields as defined in line 40. Line 50 reads a record from the file. Line 60 converts the contents of to a single-precision numeric value and assigns it to the variable MI. Similarly, line 70 converts the contents of to an integer number and assigns the value to the variable R1. Line 80 writes the vales of N, MI and R1 on the screen.
In this case, N$, M1 and R1 may represent student names, marks and class rank. Note the conversion functions do not change the actual data. They only change the way the data is interpreted.
Remember that B$ and C$ were originally numbers which would have been written to the file using the MKS$ and MKI$ functions. Example 6 will help us in understanding the technique of accessing data stored in random life.
Example 6
Write a program to access the random file "SALARY" that was created in example 5.
You are required to print the data contained in any desired record along with the total salary.
Program
10 REM ** ACCESSING A RANDOM FILE **
20 OPEN "R" # 1, " SALARY", 60
30 FIELD # 1, 20 AS N$, 10 AS B$, 10 AS D$, 10 AS H$, 10 AS C$
40 INPUT "RECORD NUMBER' K
50 IF K=0 THEN 130
60 GET # 1, K
70 LET P = CVS (B$)
75 LET Q =CVS (D$ )
80 LET R =CVS (H$)
85 LET S = CVS (C$)
90 LET T = P+Q+R+S
100 PRINT "Record" K, N$, .P, .Q, .R, .S, .T
110 PRINT
120 GOTO 40
130 CLOSE
140 END
Output
RECORD NUMBER? 40
RECORD 40 ANAMIKA 15000 3000 6000 1000 25000
RECORD NUMBER? 1
RECORD 1 UPMA 25000 5000 8000 2000 40000
RECORD NUMBER? 10
RECORD 10 SANJU 20000 4000 7000 1500 32500
RECORD NUMBER? 0
IN-TEXT QUESTIONS 1
1. Determine whether the following statement to true or false
(a) A file can store a program's output for future use
(b) A file cannot be used by more than one program.
(c) When a file is opened for INPUT, data can be written from the program to the file.
(d) If a sequential file is created and then read in the same program, it must be closed and reopened between these operations.
2. Correct the syntax errors in each statement.
(a) 10 OPEN "FILE 1" AS # 2 FOR OUTPUT
(b) 20 WRITE # 2 A, B, C, D,
(c) 30 KILL FILE 2
3 Following program segment is supposed to create and display (on the screen) in file containing the integers from 1 to 100, but it does not run properly. Correct the errors.
10 OPEN "FILE1" FOR INPUT AS # 1
20 FOR I = 1 TO 100
30 PRINT # 1, I
40 NEXT I
50 FOR I= 1 TO 100
60 INPUT # 1, I
70 PRINT # 2, I
80 NEXT I
90 CLOSE # 1
100 END
4. Write a program in BASIC to create a file called "STUDENT' containing the following data:
(a) Name of the student
(b) Roll Number
(c) Marks in LOTUS
(d) Marks in dBASE
(e) Marks in BASIC
5. Write a program to retrieve data from the "STUDENT' file created in question 4 above and copy the same in a file named "STUDENT"1.
28.6 WHAT YOU HAVE LEARNT
In this lesson, we have discussed about processing of both sequential and random files. Although, now-a-days advance programming languages are used to process files, file handling through BASIC is quite simple and useful. Even today, BASIC files are quite popular in business data processing. It is hoped that the fundamentals and the examples presents in this lesson will be helpful in understanding the various steps involved in file processing and also to develop your own programs.
28.7 FEEDBACK TO IN-TEXT QUESTION
IN-TEXT QUESTIONS 1
1.
(a) True
(b) False
(c) False
(d) True
2
(a) 10 OPEN "FILE1" FOR OUTPUT AS # 2
(b) 20 WRITE # 2, A, B, C, D
(c) 30 KILL "FILE 2"
3.
CHANGE:
10 OPEN "FILE1" FOR OUTPUT AS # 1
70 PRINT I
INSERT:
45 CLOSE # 1
47 OPEN "FILE1" FOR INPUT AS # 1
4.
10 REM PROGRAM TO CREATE " STUDEN" FILE
20 OPEN "STUDENT" FOR OUTPUT AS # 1
30 PRINT "NAME, ROLLNO, LOTUS-MARKS, DBASE-MARKS, BASIC- MARKS"
40 PRINT "Type AAA, 0,0,0,0 TO MAKE AN END"
50 INPUT N$, R, L, D, B
60 IF N = "AAA" THEN 90
70 WRITE # 1, N$, R, L, D, B
80 GO TO 50
90 CLOSE # A
100 END
5.
10 REM PROGRAM TO COPY "STUDENT" FILE
20 OPEN "STUDENT" FOR INPUT AS # 1
30 OPEN "STUDENT1 FOR OUTPUT AS # 2
40 IF EOF (1) THEN 80
50 INPUT # 1, N$, R, L, D, B
60 WRITE # 2, N$, R, L, D, B
70 GO TO 40
80 CLOSE # 1
90 CLOSE # 2
100 END
28.8 TERMINAL QUESTIONS
1. Find the syntax errors in the following statements:
10 OPEN "FILES" AS #5 FOR OUTPUT
20 PRINT #6 A; ","; B$; ";"; PAY
30 INPUT #2, A: B: C
40 OPEN #2 LEN = 128
50 FIELD #1, A$ AS 10
2. The following program segment is supposed to create and display a file of numbers. Correct the errors in it.
10 OPEN "NUMFL" FOR INPUT AS #1
20 FOR I = 1 TO 10
30 READ N
40 PRINT #3, N
50 NEXT K
60 FOR K = 1 TO 10
70 WRITE #1, N
80 PRINT N
90 NEXT I
100 CLOSE #1
110 DATA 5, 7, 9, 10, 12, 15, 17, 19, 20, 22
120 END
3. Suppose that a file named COMPUTE.DTA exists with 20 records of the form:
| NM$ | COST$ |
Correct the following program so that it displays the fifth record in this file on the screen:
10 OPEN "COMPUTER.DTA" A$ #1 LEN = 24
20 FIELD #1, 4 AS COST $, 20 AS NM$
30 GET #2, 5
40 LET NM$ = N$
50 LET COST$ = COST
60 PRINT #1, NM$, COST
4. Write a program segment that performs the following operations:
(a) Display the number of record of records in EMPLOYEE.DAT.
(b) Delete the fourth record from the file.
(c) Change the pay in the sixth record from Rs. 1500 to Rs. 2500.
5. Assume that a file PAYROLL exists with records with form Employee number (NUM), Name (NM$), Rate of pay (RATE), and a sentinel record of 0, "AAA", 0. Write a program segment that
(a) Renames this file as PAY.
(b) Deletes a record with employee number 50.
(c) Adds the records 135, "SANJU", 12
(d) Change the rate of pay of employee number 140 from 15 to 20.