FILE MANAGEMENT
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.
OBJECTIVES
On completion of this lesson, you should be able to:
· understand various types of files
· know how to handle sequential files
· know how to handle random access files
· discuss various examples/ programs relating to these two type of files.
TYPES OF FILES
Depending on the way in which the data is stored and accessed, files are categorised as:
· Sequential access files
· Direct (Random) access files
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.
Consider an inventory file shown in Table 1.1
Part name Part number Price Quantity
AAAA 101 8.00 100
BBBB 102 10.00 200
CCCC 103 12.00 300
Table 1.1 Inventory file
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.
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.
OBJECTIVES
On completion of this lesson, you should be able to:
· understand various types of files
· know how to handle sequential files
· know how to handle random access files
· discuss various examples/ programs relating to these two type of files.
TYPES OF FILES
Depending on the way in which the data is stored and accessed, files are categorised as:
· Sequential access files
· Direct (Random) access files
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.
Consider an inventory file shown in Table 1.1
Part name Part number Price Quantity
AAAA 101 8.00 100
BBBB 102 10.00 200
CCCC 103 12.00 300
Table 1.1 Inventory file
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.
HANDLING OF SEQUENTIAL FILES
Using data files would require the incorporation of statements in your program to perform the following functions:
· Giving a name to the file
· Opening the file
· Writing to or reading from the file
· Closing the file
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.
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.
HANDLING OF SEQUENTIAL FILES
Using data files would require the incorporation of statements in your program to perform the following functions:
· Giving a name to the file
· Opening the file
· Writing to or reading from the file
· Closing the file
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
1. Extension is longer than three characters.
2. Name is longer than eight characters.
3. Name is longer than eight characters and extension is included.
Result
1. Extra characters are truncated.
2. A period is inserted after the eighth character and extra characters (up to three) are used as the extension.
3. Gives an error.
1. Extra characters are truncated.
2. A period is inserted after the eighth character and extra characters (up to three) are used as the extension.
3. Gives an error.
For example,
SURENDERKUMAR becomes SURENDER.KUM.
PROGRAM.PENSION becomes PROGRAM.PEN.
PAYROLLCALUCTION.BAS gives an error.
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
· O for sequential output file
· I for sequential input file
· R for random input/output file
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.
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
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 PART PRICE QUANTITY VALUE
NAME NUMBER
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
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
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
SURENDERKUMAR becomes SURENDER.KUM.
PROGRAM.PENSION becomes PROGRAM.PEN.
PAYROLLCALUCTION.BAS gives an error.
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
· O for sequential output file
· I for sequential input file
· R for random input/output file
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.
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
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 PART PRICE QUANTITY VALUE
NAME NUMBER
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
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
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

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home