Wednesday, 27 February 2019
2019 SS2 WEEK 7 TOPIC:HANDLING COMPUTER FILES
When you think of a file, don’t think of a paper file at all. Instead, the file is a container. The container can be very large or small. Unlike a printed sheet of paper, a file can contain a variety of stuff. The container keeps that stuff together and separate from other containers, which also contain stuff separate from other containers.
Basic Operations on Computer Files
Files on a computer can be created, moved, modified, grown, shrunk and deleted. In most cases computer programs that are executed handles these operations, but the user of a computer can also manipulate files if necessary.
The following are some of the basic file operations:
1. Create: To make new file
2. Delete: To remove a file from disk
3. Retrieve: To find a file and bring it back
4. Copy: To reproduce a file so it can have same content as original file
5. View: See the files in a folder
6. Update: To make something more suitable for use now adding new information or changing its content
7. Open: To open a file for editing
8. Close: To close the edited file
Creating a sequential file
There are several ways to organize data in sequential file. The technique presented here is easy to implement.
1. Choose a DOS file name. A DOS file name is a string consisting of a base name of at most eight characters followed by an optional extension consisting of a period and at most three characters
2. Choose a number from 1through 255 to be the reference number of the file
3. Execute the statement
OPEN filename FOR OUTPUT AS #n
Where n is the reference number
4. Place data into the file with the WRITE* statement. If a$ is a string, then the statement WRITE #n. a$
Writes the string surrounded by quotation marks into the file. If c is a number, then the statement
WRITE #n, c
5. After all the data have been recorded in the file, execute
CLOSE #n
Where n is the reference number. This statement breaks the communication line with the file and dissociates the number n from the file. This procedure is referred to as closing a file
ACCESS A SEQUENTIAL FILE
Data stored in a sequential file can read in order and assigned to variables with the following steps:
1. Choose a number from 1 through 255 to be the reference number of the file. This number is not necessary to be the same number that was used when the file was recorded.
2. Execute the statement
OPEN filename FOR INPUT AS #n
Where n is the reference number. This procedure is referred to as opening a file for input. It establishes a communication line between the computer and the disk drive for reading data from the diskette.
3. Read data from the file with the INPUT* statement. INPUT* statement assigns data from file to variable.
INPUT #n. var1, var2 ,…….
4. After the desired items have been found or all the data has been read from the file, close the file with the statement CLOSE #n.
Basic function EOF; it tells us if we have reached the end of a file. For example the condition EOF (n) will be true if the end of file n has been reached and false otherwise.
EXAMPLE: Write a program to display a table showing the ages in 1991 of the people in the sequential file YOB.DAT.
Solution
10 REM Process data from YOB.DAT file to find ages in 1991
CLS
20 OPEN “YOB.DAT” FOR INPUT AS #5
30 PRINT “NAME”, “Age in 1991”
40 DO WHILE NOT EOF (5)
50 REM Process the entire file
60 INPUT #5, name$, year
70 PRINT name$, 1991 – year
80 REM Display name and age in 1991
90 LOOP
100 CLOSE #5
END
[run]
Name Age in 1991
Barbra 49
Ringo 51
Sylvester 45
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment