Chapter-9
File Handling in QBASIC
1. File Handling:-
File handling is a process to create a data file, write data to the data file and read data
from the specified data file.
2. File:
File is the collection of different data, instructions or information, which is stored in
the secondary storage device (File name must be unique).
3. Types of File:-
QBASIC uses two types of file i.e.
a. Program file: It contains a set of instructions that are needed for data processing.
A program file has .BAS extension.
b. Data file: It contains only data that are required during data processing. Data files
may contain many records. A data file can have any extensions like .dat, .txt, etc.
a. Types of Data files:
i. Sequential Access Data File: Data are stored and retrieved in the
sequential order. It is easy to create and simple to understand but we
cannot access the particular data directly from the data file so data
processing becomes slow for more records.
ii. Random Access Data File: Data are stored and retrieved in random
order. A record of any position can be accessed directly. It is faster than
sequential Access Data File for reading and writing the data.
4. Basic File Operations:
There are basically three file operations we can perform in QBASIC. They are:
a. Opening the File
b. Read from the File/Write into the File.
c. Close the File.
5. Modes of Operations:
a. OUTPUT Mode
b. INPUT Mode
c. APPEND Mode
6. Purpose of file mode:
Mode Purpose
OUTPUT or “O” i. It creates a new sequential data file.
ii. And write data to the file.
INPUT or “I” i. To read / display data or information
from the existing sequential data file.
APPEND or “A” i. To add more records in the existing
sequential file.
ii. If the spe4cified data file does not
exist, APPEND mode creates it.
7. Difference between OUTPUT and APPEND mode:
OUTPUT Mode APPEND Mode
This mode is used to write data into a This mode is used to write data into an
new file. existing as well as new file.
The record pointer will be always on The record pointer will be always on the
first record. last record.
38 | P a g e
8. Difference between INPUT and OUTPUT Mode:
INPUT Mode OUTPUT Mode
This mode is to read entire data from an This mode is used to store data into new
existing data file. or fresh file.
It is based to output mode in order to It is not based with input mode because
access and process data. it simply stores data.
9. Statements / Keywords/Commands used in sequential data file:
a. OPEN Statement:
This statement is used to name and open a data file.
Syntax: OPEN “File name” FOR Mode AS #File number. Where,
Mode is the purpose of opening data files.
Example: OPEN “sales.txt” FOR OUTPUT AS #1 or
OPEN “O”, #1, “sales.txt”
b. INPUT Statement:
This statement is used to enter data from the keyboard while running the
program.
Syntax: INPUT “message”; Variable.
Example: INPUT “What is your name”; name$
c. WRITE # Statement (Writing on a file):
This statement is used to write one or more data in the specified sequential
file.
Syntax: WRITE# File number, Variable lists
Example: WRITE #5, n$, a$, s
d. PRINT # Statement:
This statement is used to write one or more data in the specified sequential
file.
Its function is same as WRITE # Statement.
Syntax: PRINT #file number, variable lists
Example: PRINT #5, n$, a$, s
e. CLOSE # Statement:
This statement is used to close one or more opened sequential data file.
Syntax: CLOSE #File numbers
Example: CLOSE #5 or CLOSE #1, #2
10. Difference between PRINT # and WRITE #.
PRINT # WRITE #
It adds spaces between data items while It inserts commas between the data
storing data. items
It does not enclose strings in double It encloses strings in double quotation
quotation marks. marks.
11. Some Basic Programs:
a. REM Program to store name, age, and salary in a newly created file
“emp.txt”
CLS
OPEN “emp.txt” FOR OUTPUT AS #5
INPUT “Enter name, age and salary”; n$, a, s
WRITE #5, n$, a, s
CLOSE #5
END
39 | P a g e
b. WAP to create a data file “STD.TXT” to store name and marks obtained in
English, math and science subjects of five students.
CLS
OPEN “stud.txt” FOR OUTPUT AS #1
FOR I = 1 to 5
INPUT “Enter name and marks in English, math and science”; n$, e, m, s
WRITE #1, n$, e, m, s
NEXT I
CLOSE #1
END
12. Questions will ask in Given Pattern in Exam:
a. WAP to store records 5/20/100 persons. If in a questions asked to store/ write
some specified number of records then use FOR….NEXT Loop.
Example: FOR I = 1 to 5 (for 5 records)
b. If the program ask to write / store records according to users choice or need then
use GOTO statements methods.
13. Exam Oriented Questions:
a. WAP to store the Data including name, age, address and phone numbers of person
in file “record.txt”. Make option for more request.
CLS
OPEN “record.txt” FOR OUTPUT AS #1
Top:
INPUT “Enter name, age, address and phone number”; n$, a, add$, P#
WRITE #1, n$, a, add$, p#
INPUT “Do you want to add more records (Y/N)”; ch$
IF ch$ = “y” OR ch$ = “Y” THEN GOTO Top
CLOSE #1
END
b. WAP to add five more students name and marks in three different subject into
existing file “mark.dat”.
CLS
OPEN “mark.dat” FOR APPEND AS #1
FOR I = 1 TO 5
INPUT “Enter name and marks in three subject”; n$, a, b, c
WRITE #1, n$, a, b, c
NEXT I
CLOSE #1
END
c. WAP to insert user need records to an existing data file “record.txt” including
name, post and salary.
CLS
OPEN “record.txt” FOR APPEND AS #1
Top:
INPUT “Enter name, post and salary”; n$, p$, s
WRITE #1, n$, p$, s
INPUT “Want to add more records (Y/N)”; ch$
IF ch$ = “y” OR ch$ = “Y” THEN GOTO Top
CLOSE #1
END
d. WAP to create a data file “teldir. Dat” to store name, address and telephone
number of employers according to the need of the users.
40 | P a g e
e. A sequential data file called “student.dat” contains some records under fields
name, marks in English, Nepali and computer. Write a program to add some more
records in the same sequential data file.
f. WAP to create a sequential file “store.dat” and store item code, item name and
rate of 50 records.
g. WAP to store records regarding the information of book number, book’s name
and writer’s name in a sequential data file called “library.dat”.
h. WAP that asks student’s name, roll and class and stores into “class.dat” only those
records who are studying in class 10. User can supply the records as per his/her
need.
i. WAP to ask students’ name, class, and marks secured in three subjects. Store the
data in a sequential data file “result.dat” along with the total marks. Make a
provision to ask the user to enter another record.
j. A sequential data file “student.dat” contains few records under the fields name,
English, Nepali and computer. WAP to add few more records in the same
sequential data file.
k. A sequential data file “record.dat” and store name, address and salary of
employees. WAP to add some more records in the data file “record.dat”. Program
should terminate with user choice.
l. Create a sequential data file “price.dat” to store item name, quantity and rate also
calculate total amount (total = quantity x rate). Program should terminate
according to the user’s choice.
m. Create a sequential data file “post.dat” to store name and marks of any three
subjects also calculate total and percentages only for 15 students.
n. Create a sequential data file “std.dat” to store name and marks obtained in
English, Math and Science subjects for a few students.
o. WAP to store records regarding the information of Book Number, Book’s name
and Writer’s name in a sequential data file called “Library.dat”.
p. Create a data file to store the records of few employees having Name, Address,
Post, Gender and Salary fields.
14. Programs related to INPUT MODE:
Students must know about some commands to write programs in INPUT
MODE.
INPUT mode is used to read the records from the sequential data file and do
processing like display, count, search and delete records from that file.
15. Some important statements:
a. INPUT # Statement: This statement is used to read specific data from the data file
and assigned them to the related variable.
Syntax: INPUT #File number, variable lists
Example: INPUT #1, n$, p$, g$, s
b. PRINT Statement: This statement is used to display the data / result on the screen
during execution.
PRINT “The records are”; n$, p$, g$, s
16. EOF (END OF FILE)
This function is used to test for the end of file position in the data file. It is very much
useful while the numbers of records are unknown in the data file.
Syntax: EOF(File number)
Example: EOF(1)
41 | P a g e
17. INPUT MODE QUESTIONS:
a. A sequential data file “emp.txt” contains some records on field name, post, gender
and salary. WAP to display all the records from that file.
b. A sequential data file “emp.txt” contains some records on field name, post, gender
and salary. WAP to display all the records whose salary is more than 25000.
c. A sequential data file “result.dat” contains name and marks secured by students in
3 subject. Assuming the pass marks of each subject is 40, WAP to display all the
records of passed students.
d. A sequential data file “emp.dat” contains some records on field name, post,
gender and salary. WAP to display all the records of employees along with tax
amount also. (tax is 20% of salary)
e. A file “student.txt” contains records on the field roll, name and marks in English,
Nepali and Maths. WAP to count the number of records on that file.
f. A file “student.txt” contains records on the field roll, name and marks in English,
Nepali and Maths. WAP to count the number of records who has got more than 70
marks in Nepali.
g. A sequential data file “emp.dat” contains employee’s name, address, gender and
salary. WAP to display all the information of employees whose salary is more
than Rs. 20,000.
h. WAP to update the rate by increasing 10% from a sequential data file “Data.dat”
that store item name, rate and quantity.
i. A sequential data file “person.txt” contains information such as name, address and
DOB of 20 People. WAP to display only those records whose age is below 18 and
address is Lalitpur.
18. Some Additional Commands:
Files: This command is used to display all the files of current drive.
Syntax: Files [File specification]
Shell: This command is used to go to DOS prompt temporarily.
NAME: This command is used to change the old file into new file.
Syntax: Name " old file name" AS "New file name"
Kill: This command is used to remove the file from the disk.
Syntax: Kill [ File Specification ]
MKDIR: This command is used to create a directory in a specific disk
Syntax: MKDIR "path"
CHDIR: This command is used to change the directory.
Syntax: CHDIR " path"
RMDIR: This command is used to remove the specific directory.
Syntax: RMDIR "path"
System: This command is used to close the Q-BASIC program.
LINE INPUT#: This statement reads entire line having maximum 255 characters and
stores to a single string variable.
42 | P a g e
19. Copy Data Program;
A sequential data file name “abc.dat” has several records having fields name, roll and
class. WAP to copy all the records of class 10 into a newly created file new. Dat.
OPEN “abc.dat” FOR INPUT AS #1
OPEN “new.dat” FOR OUTPUT AS #2
CLS
WHILE NOT EOF(1)
INPUT #1, N$, R, C
IF c = 10 THEN WRITE #2, N$, R, C
WEND
CLOSE #1, #2
END
20. Delete Record Program:
Delete some records from “neps.dat” file where computer ask user to enter the record,
which is to be deleted. (Fields are name, address, and telephone number)
OPEN “neps.dat” FOR INPUT AS #1
OPEN “Temp.dat” FOR OUTPUT AS #2
CLS
INPUT “Enter name which is to be deleted”; D$
WHILE NOT EOF(1)
INPUT #1, N$, A$, T#
IF UCASE$(D$)< >UCASE$(N$) THEN
WRITE #2, N$, A$, T#
ELSE
PRINT “Deleted data = “; N$, A$, T#
ENDIF
WEND
CLOSE #1, #2
KILL “Neps.dat”
NAME “Temp.dat” AS “Neps.dat”
END
43 | P a g e