You are on page 1of 19

File Handling

1. File Handling
File handling is a process to create, write, modify and read data to the data file.
2. Data file
The file which contains data given by the user to the program and information
provided by the user to the computer is called data file.
The different types of data file are:
a) Sequential Access files –
 A data file that stores a sequential data file containing name, address of some
people in the same order or in sequential order is called sequential access data
file.
 It takes long time to access if the data file contains a large volume of data.
 In sequential access data file we cannot change the existing entry or insert a
new entry.
b) Random access files

 A data file that allows to write or read data from any location of the
file.
 It means we can read or write any record directly in a random
without searching through all the records that precede it.
 Thus, reading and writing of data is faster.
3. Program File
The file which contains a set of instructions that are needed for data processing is
called program file.
4. Mode of data file [SLC 2069]
Mode of data file means opening a sequential file for one of the three modes of
operation like output mode, input mode and append mode.
5. Modes of operation for opening a sequential file
a) Output Mode: It is used to create a new data file and write data in it. If the file
already exists its current contents will be destroyed.(store)
b) Input Mode: It is used to retrieve records or contents of existing data file. (display)
c) Append Mode: It is used to add more records in existing file. If the specified file
does not exist APPEND mode creates it. (add more records)

6. File number
The number assigned to a file in order to identify it during processing is called file
number.
Write down the functions of: (1 Marks sure)
7. OPEN statement: It opens a sequential file for one of the three possible operations (reading,
writing, appending).
8. WRITE statement [SEE 2075] [MM 2076]: It sends one or more data items to the
specified file. It inserts commas between the data items. It encloses strings in double
quotation marks.
9. PRINT#: It adds spaces between data items while storing data. It does not enclose strings in
double quotation marks.
10. CLOSE statement [SEE 2074] [SEE 2073: It closes one or all open files.
11. INPUT# statement [SEE 2075 U] [SEE 2075 S 2]: It reads data from the sequential data
file.
12. EOF( ) function [SEE 2075 S 2]: Eof stands for end of file.It is used to test the end of
records with in a file.
13. LINE INPUT statement: It reads the entire line or maximum 255 characters form the
keyboard or the sequential file.
14. INPUT$ function: It reads the specified number of characters form the data file.
15. NAME statement [SEE 2073] [SLC 2071 S]: The NAME statement renames a file on a
diskette. Only file name changes, data and program line remains intact.
16. KILL statement [SEE 2074]: The KILL statement deletes the file or files from the
specified drive and directory.
17. MKDIR statement [SEE 2075 U]: It creates a subdirectory which is used to manage files.
18. CHDIR statement: It allows QBASIC to change from one directory to another.
19. RMDIR statement [SLC 2065] [SLC 2068 S]: It is used to remove or delete only the
subdirectories from a disk. It can remove only empty subdirectories.
20. FILES statement: The FILES statement displays the files of the current sub directory or
specified sub directory.
21. SHELL: The SHELL is used to exit temporarily form the QBASIC environment to DOS
prompt.
22. SYSTEM: It closes all the files and exits permanently form QBASIC.

Syntax:-
To open file
Method 1:
OPEN <filename> For <Mode> AS #<filenumber>
Method 2:
OPEN <mode>,#<filenumber>,filename
Example:-
OPEN "Student.bas" FOR OUTPUT AS #1
OPEN "O",#1,"Student.bas"
OPEN "Teacher.bas" FOR INPUT AS #23
OPEN "I",#23,"Teacher.bas "
OPEN "School.bas" FOR APPEND AS #2
OPEN "A",#2,"School.bas"

1. WAP to create data file name "Teacher.dat" to store Information of teacher on the filed
Teacher_id, Name, Address, Salary and Phone number for a single teacher.
CLS
OPEN "Teacher.dat" FOR OUTPUT AS #5
INPUT "Enter the teacher id"; T
INPUT "Enter name of teacher"; N$
INPUT "Enter Address";A$
INPUT "Enter salary";S
INPUT "Enter phone number";P
WRITE #5,T,N$,A$,S,P
CLOSE #5
END

2. WAP to create data file name "Teacher.dat" to store Information of teacher on the filed
Teacher_id, Name, Address, Salary and Phone number for five teacher.
CLS
OPEN "Teacher.dat" FOR OUTPUT AS #5
For I=1 to 5
INPUT "Enter the teacher id"; T
INPUT "Enter name of teacher"; N$
INPUT "Enter Address";A$
INPUT "Enter salary";S
INPUT "Enter phone number";P
WRITE #5,T,N$,A$,S,P
Next I
CLOSE #5
END

(IMP)3. WAP to create data file name "Teacher.dat" to store Information of teacher on the
filed Teacher_id, Name, Address, Salary and Phone number until the user wants.

CLS
OPEN "Teacher.dat" FOR OUTPUT AS #5
Add:
INPUT "Enter the teacher id"; T
INPUT "Enter name of teacher"; N$
INPUT "Enter Address";A$
INPUT "Enter salary";S
INPUT "Enter phone number";P
WRITE #5,T,N$,A$,S,P
INPUT "Do you want to add more records(Y/N)";C$
IF UCASE$(C$)="Y" THEN GOTO Add
CLOSE #5
END

4. WAP to create a data file name "Student.txt" to store roll no, name, marks of three subject
(Maths, Science and English) of any one student.
OPEN "Student.txt" FOR OUTPUT AS #3
CLS
INPUT "ENTER ROLL NO";R
INPUT "ENTER NAME";N$
INPUT "ENTER MATHS MARKS";M
INPUT "ENTER SCIENCE MARKS";S
INPUT "ENTER ENGLISG MARKS";E
WRITE #3,R,N$,M,S,E
CLOSE #3
END

5. WAP to create a data file name "Student.txt" to store roll no, name, marks of three subject
(Maths, Science and English) of any 10 students.
OPEN "Student.txt" FOR OUTPUT AS #5
CLS
FOR I=1 TO 10
INPUT"ENTER ROLL NO"; R
INPUT"ENTER NAME"; N$
INPUT"ENTER MATHS MARKS";M
INPUT"ENTER SCIENCE MARKS";S
INPUT "ENTER ENGLIGH MARKS";E
WRITE #2,R,N$,M,S,E
NEXT I
CLOSE #5
END

6. WAP to create a data file name "Student.txt" to store roll no, name, marks of three subject
(Maths, Science and English) until user wants.
OPEN "Student.txt" FOR OUTPUT AS #5
CLS
ADD:
INPUT"ENTER ROLL NO"; R
INPUT"ENTER NAME"; N$
INPUT"ENTER MATHS MARKS";M
INPUT"ENTER SCIENCE MARKS";S
INPUT "ENTER ENGLIGH MARKS";E
WRITE #2,R,N$,M,S,E
INPUT"DO YOU WANT TO ADD MORE RECORS(Y/N)";C$
IF UCASE$(C$)="Y" THEN GOTO ADD
CLOSE #5
END

7. A data file name "Teacher.dat" contains information about teacher under the field
Teacher_id, Name, Address, Salary and Phone number. WAP to add some records in it
without deleting existing records.
CLS
OPEN "Teacher.dat" FOR APPEND AS #5
Add:
INPUT "Enter the teacher id"; T
INPUT "Enter name of teacher"; N$
INPUT "Enter Address"; A$
INPUT "Enter salary";S
INPUT "Enter phone number";P
WRITE #5,T,N$,A$,S,P
INPUT "Do you want to add more records(Y/N)";C$
IF UCASE$(C$)="Y" THEN GOTO Add
CLOSE #5
END

8. A data file name "Student.txt" have some records of students in the field as roll no, name,
marks of three subject (Maths, Science and English). WAP to add some more records of
students in the same existing file without deleting any records.
OPEN "Student.txt" FOR APPEND AS #1
CLS
ADD:
INPUT"ENTER ROLL NO"; R
INPUT"ENTER NAME"; N$
INPUT"ENTER MATHS MARKS";M
INPUT"ENTER SCIENCE MARKS";S
INPUT "ENTER ENGLIGH MARKS";E
WRITE #1,R,N$,M,S,E
INPUT"DO YOU WANT TO ADD MORE RECORS(Y/N)";C$
IF UCASE$(C$)="Y" THEN GOTO ADD
CLOSE #1
END
9. A data file name "Teacher.dat" contains information about teacher under the field
Teacher_id, Name, Address, Salary and Phone number. WAP to display all the records
from the file.
OPEN "Teacher.dat" FOR INPUT AS #20
CLS
PRINT "Teahcer_Id", " Name", " Address", " Salary", "Phone number"
WHILE NOT EOF(20)
INPUT #20, T, N$, A$, S, P
PRINT T, N$, A$, S, P
WEND
CLOSE #20
END

10. A data file name "Student.txt" have some records of students in the field as roll no,
name, marks of three subject (Maths, Science and English). WAP to display all the records
from the file.
OPEN "Student.txt" FOR INPUT AS#3
CLS
PRINT "Roll no","Name","Maths","Science","English"
WHILE NOT EOF(3)
INPUT #3,R,N$,M,S,E
PRINT R,N$,M,S,E
WEND
CLOSE #3
END

11. A data file name "Teacher.dat" contains information about teacher under the field
Teacher_id, Name, Address, Salary and Phone number. WAP to display all the records
from the file along with the tax that individual teacher had to pay. (Tax:2% of salary)
OPEN "Teacher.dat" FOR INPUT AS#2
CLS
PRINT "Teacher_id","Name","Address","Salary","Phone number", "Tax"
WHILE NOT EOF(2)
INPUT #2,T,N$,A$,S,P
Tax=(2/100)*S
PRINT T,N$,A$,S,P,Tax
WEND
CLOSE #2
END

12. A data file name "Student.txt" have some records of students in the field as roll no,
name, marks of three subject (Maths, Science and English). WAP to display all the records
from the file with total marks.
OPEN "Student.txt" FOR INPUT #4
CLS
PRINT "ROLL","NAME","MATHS","SCIENCE","ENGLISH","TOTAL"
WHILE NOT EOF(4)
INPUT #4,R,N$,M,S,E
Total=M+S+E
PRINT R,N$,M,S,E,Total
WEND
CLOSE #4
END

13. A data file name "Student.txt" have some records of students in the field as roll no,
name, marks of three subject (Maths, Science and English). WAP to display all the records
of students along the percent that individual students has scored.
OPEN "Student.txt" FOR INPUT #4
CLS
PRINT "ROLL","NAME","MATHS","SCIENCE","ENGLISH","PERCENT"
WHILE NOT EOF(4)
INPUT #4,R,N$,M,S,E
Total=M+S+E
Per=Total/3
PRINT R,N$,M,S,E,Per
WEND
CLOSE #4
END

15. A data file name "Teacher.dat" contains information about teacher under the field
Teacher_id, Name, Address, Salary and Phone number. WAP to display all the records
from the file with total number of records.
OR
A data file name "Teacher.dat" contains information about teacher under the field
Teacher_id, Name, Address, Salary and Phone number. WAP to count total number of
records in the file and display it.
OPEN "Teacher.dat" FOR INPUT AS #3
PRINT"TEAHCE_ID","NAME","ADDRESS","SALARY","PHONE NUMBER"
WHILE NOT EOF(3)
INPUT #3,T,N$,A$,S,P
PRINT T,N$,A$,S,P
COUNT=COUNT+1
WEND
PRINT "The total number of records is";COUNT
CLOSE #3
END

16. A data file name "Teacher.dat" contains information about teacher under the field
Teacher_id, Name, Address, Salary and Phone number. WAP to display records from the
file whose Name is Ram and salary is greater than 2000.

OPEN "Teacher.dat" FOR INPUT AS #4


CLS
PRINT "Teacher_id","Name","Address","Salary","Phone_number"
WHILE NOT EOF(4)
INPUT #4,T,N$,A$,S,P
IF LCASE$(N$)="ram" AND S>2000 THEN
PRINT T,N$,A$,S,P
END IF
WEND
CLOSE #4
END

17. A data file name "Student.txt" have some records of students in the field as roll no,
name, marks of three subject (Maths, Science and English). WAP to display all the
records of students whose name is Sita and score more than 50 in Maths.
OPEN "STUDENT.TXT'' FOR INPUT AS #2
CLS
PRINT "ROLL","NAME","MATHS","SCIENCE","ENGLISH"
WHILE NOT EOF(2)
INPUT #2,R,N$,M,S,E
IF LCASE$(N$)="sita" AND M>50 THEN
PRINT R,N$,M,S,E
END IF
WEND
CLOSE #2
END

18. A sequential data file “SALARY.DAT” contains the information, Employee-Code,


Employee-Name, Post, Basic-Salary. Write a program to display those records whose
Basic-salary is between 10000 to 15000 and Post is ‘OFFICER’.
OPEN "SALARY.DAT" FOR INPUT AS #3
CLS
PRINT "Emplyoee_code","Employee_name","Post","Salary"
WHILE NOT EOF(3)
INPUT #3,C,E$,P$,S
IF S>10000 AND S<15000 AND UCASE$(P$)="OFFICER" THEN
PRINT C,E$,P$,S
END IF
WEND
CLOSE #3
END

19. Write a program that reads the ”INFO.DAT” file that has several record such as name,
address, gender, post, and salary .The program display those record whose sex is male and
salary more than 10,000.
CLS
OPEN "INFO.DAT" FOR INPUT AS #4
PRINT "Name","Address","Gender","Post","Salary"
WHILE NOT EOF(4)
INPUT #4,N$,A$,G$,P$,S
IF UCASE$(G$)="M" AND S>10000 THEN
PRINT N$,A$,G$,P$,S
END IF
WEND
CLOSE #$
END

20. A data file name “EMP.DAT”, contains number of records having fields name, post and
salary. Write a program to count total number of “Manager” in the data file. (hint: Manager
is a post)
OPEN "EMP.DAT" FOR INPUT AS #5
CLS
PRINT "NAME","POST","SALARY"
WHILE NOT EOF(5)
INPUT #5,N$,P$,S
IF UCASE$(P$)="MANAGER" THEN
PRINT N$,P$,S
Count=Count+1
END IF
WEND
PRINT "THE TOTAL NUMBER OF MANGER IS";Count
CLOSE #5
END

Rewrite the following programs after correcting


bugs:
1.
REM To store Name, Salary, Post and Address to data file "Emp.dat"
OPEN "Emp.dat" FOR INPUT AS #3
CLS
INPUT"Enter the name"; N
INPUT"Enter the salary"; S
INPUT "Enter the Address";A$
INPUT "Enter the post";P
Write #4,N$,S,A$,P
Close #2
END

Answer:-
OPEN "Emp.dat" FOR OUTPUT AS #3
CLS
INPUT "Enter the name"; N$
INPUT "Enter the salary"; S
INPUT "Enter the Address"; A$
INPUT "Enter the post"; P$
Write #3, N$,S,A$,P$
Close #3
END

2. REM To store Roll no, Name, Gender and Total marks to data file
"Student.dat" for 10 students

OPEN "Teacher.dat" FOR O As #3


CLS
FOR k= 1 to 20
INPUT "Enter roll"; R$
INPUT "Enter name", N$
INPUT "Enter Gender(M/F)"; G
INPUT "Enter total marks"; T
INPUT #3,R,Name$,G,Total
Wend
Close #4
END

Answer:
REM To store Roll no, Name, Gender and Total marks to data file
"Student.dat" for 10 students

OPEN "Student.dat" FOR OUTPUT As #3


CLS
FOR k= 1 to 10
INPUT "Enter roll"; R
INPUT "Enter name", N$
INPUT "Enter Gender(M/F)"; G$
INPUT "Enter total marks"; T
WRITE #3,R,N$,G$,T
NEXT k
Close #3
END

3.
REM To store Roll no, Name, Gender and Total marks to data file
"Student.dat" till user wants
OPEN "Student.dat" FOR Append As #22
CLS
Top:
INPUT "Enter roll"; R$
INPUT "Enter name", N$
INPUT "Enter Gender(M/F)"; Gender$
INPUT "Enter total marks"; T
Write #3,R,N$,G,Total
INPUT "Do you want to add more records(Y/N)"; C
IF LCASE(C$)="Y" THEN GO TO Top
END IF
Close #3
END
Ans:
REM To store Roll no, Name, Gender and Total marks to data file
"Student.dat" till user wants
OPEN "Student.dat" FOR Output As #2
CLS
Top:
INPUT "Enter roll"; R
INPUT "Enter name", N$
INPUT "Enter Gender(M/F)"; G$
INPUT "Enter total marks"; T
Write #2,R,N$,G$,T
INPUT "Do you want to add more records(Y/N)"; C$
IF LCASE$(C$)="y" THEN GO TO Top
Close #2
END

4. REM To add more records to the file name "student.dat" that have
already some records of students in the field Name, Address and Age
OPEN "STUDENT.DAT" FOR INPUT AS 1#
INPUT "NAME"; N$
INPUT "ADDRESS"; ADD$
INPUT "AGE"; AGE$
WRITE #1, N$, ADD$, AGE
END # 1
STOP

Answer:
REM To add more records to the file name "student.dat" that have already
some records of students in the field Name, Address and Age
OPEN "STUDENT.DAT" FOR APPEND AS #1
INPUT "NAME"; N$
INPUT "ADDRESS"; ADD$
INPUT "AGE"; AGE
WRITE #1, N$, ADD$, AGE
CLOSE # 1
END

5. REM program to read data from the data file.


OPEN “STD.DAT” FOR OUTPUT AS #1
CLS
WHILE NOT BOF (#1)
WRITE#1, N$, R, C, P
PRINT N$, R, C, P
WEND
CLOSE “STD.DAT”
END
Answer:
REM program to read data from the data file
OPEN “STD.DAT” FOR INPUT AS #1
CLS
WHILE NOT EOF (1)
INPUT #1, N$, R, C, P
PRINT N$, R, C, P
WEND
CLOSE #1
END

6. Rem To print only class 10 record from "student.dat"


CLS
OPEN "I",#2, "Student.Dat"
PRINT "Name", "Class", "Roll"
WHILE NOT EOF (#2)
WRITE#2, N$,C,R
IF C=10 THEN
DISPLAY N$,C,R
END IF
NEXT
CLOSE #2
END
Answer:
Rem To print only class 10 record from "student.dat"
CLS
OPEN "I",#2, "Student.Dat"
PRINT "Name", "Class", "Roll"
WHILE NOT EOF (2)
INPUT #2, N$,C,R
IF C=10 THEN
PRINT N$,C,R
END IF
WEND
CLOSE #2
END

7. REM to copy from old file "temp.dat" to new file "info.dat"


CLS
OPEN "info.dat" for INPUT AS #1
OPEN "temp.dat" for OUTPUT AS 2#
DO UNTIL EOF(1)
INPUT #2, n$, p$, d$, s
WRITE #1, n$, p$, d$, s
WEND
CLOSE #2, #2
END

Answer:
REM to copy from old file "temp.dat" to new file "info.dat"
CLS
OPEN "info.dat" for OUTPUT AS #1
OPEN "temp.dat" for INPUT AS #2
DO UNTIL NOT EOF(1)
INPUT #2, n$, p$, d$, s
WRITE #1, n$, p$, d$, s
LOOP
CLOSE #1, #2
END

You might also like