Saturday, December 28, 2019

File Handling



Introduction:-
Data are used in all the programs for retrieving information. In QBASIC you can store data in a separate file called a data file. Hence there are two types of files in data processing. They are:
Program file :A program file contains a set of instructions that are needed for data processing. A program file has BAS extention. Date file : A data file contains only data that are required during data processing. In a data file related of a person or any thing are stored in a row known as a record.

File Handling:-
It is a process to create data file,write data to the data file and read data from it. Types of File Handling Sequential Data File : It is a file that should be accessed in a sequential manner starting at the beginning of the data block and process ending in order until and end of data. Random Data File: It is a file which a key is used to point its appropriate record stored on a disk.

Some useful commands with their function and syntax are as follows :-
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.
OPEN: This statement is used to open the file.
WRITE#: This statement is used to place the data into data file.
INPUT#: This statement is used to read each data and stores to corresponding variable.
LINE INPUT#: This statement reads entire line having maximum 255 characters and stores to a  single string variable.
EOF ( ) : This statement is used to continue looping till the end a file.It means{ End of file }
Syntax: EOF ( File number)

Modes of operations of sequential access data file:

You need to open a sequential access data file in order to use it. A sequential data file can be opened in one of three modes.

 Types of Mode:

OUTPUT mode : A file is opened in output mode to write data to the file. When a file is opened in output mode BASIC always creates a new file. If a file with the name already exists then it will overwrite without warning.

INPUT mode : A file is opened in input mode to read data from the file. The file must exist to open it in input mode otherwise error message "file not found" is displayed.

APPEND mode : A file is opened in append mode to add data to an existing file. New data are added to the end of existing file. It is recommended mode for writing data to a file unless there is specific reason to create new.


1. Create a sequential data file “STUDENT.dat” to store name and marks obtain in English, Maths and Science for a few students.



Ans:

CLS
OPEN “STUDENT.DAT” FOR OUTPUT AS #1
DO
INPUT “Enter name”; N$
INPUT “Enter marks in English”; E
INPUT “Enter marks in Maths”; M
INPUT “Enter marks in Science”; S
WRITE #1, N$, E, M, S
INPUT “Do you want to continue(Y/N)”; CH$
LOOP WHILE UCASE$(CH$) =”Y”
CLOSE #1
END





2.Write a program to open a data file “STUDENT.DAT” that contains Name, Address, Telephone number and Parent’s Name of some students. Now display all those records whose Address is “LALITPUR”.


Ans:

CLS
OPEN “STUDENT.DAT” FOR INPUT AS #1
WHILE NOT EOF(1)
INPUT #1, N$, A$, T#, PN$
IF UCASE$(A$) =”LALITPUR” THEN PRINT N$, A$, T#, PN$
WEND
CLOSE #1
END

3. A sequential data file named “Nabil.txt” contains record of clients of a bank including depositor’s name, deposited amount, time and rate of interest. WAP to display detail of all depositors including simple interest.

Ans:

CLS
OPEN “NABIL.TXT” FOR INPUT AS #1
WHILE NOT EOF(1)
INPUT #1, N$, A, T, R
I=(A*T*R)/100
PRINT N$, A, T, R, I
WEND
CLOSE #1
END

4.WAP that asks a post of the employee and displays his/her records from the sequential data file “XYZ.REC” having fields Name, Post, Dept and Salary.


Ans:

CLS
OPEN “XYZ.REC” FOR INPUT AS #1
INPUT “Enter post to be searched”; S$
FLAG=0
WHILE NOT EOF(1)
INPUT #1, N$, P$, D$, S
IF UCASE$(S$)=UCASE$(P$) THEN
PRINT N$, P$, D$, S
FLAG=1
END IF
WEND
IF FLAG=0 THEN PRINT “Data not found”
CLOSE #1
END




5.WAP to store the information of book's name and writer's name, price and quantity in a sequential data file called "RECORD.DAT"

Ans



   CLS
   OPEN "RECORD.DAT" FOR OUTPUT AS #1
   TOP:
   INPUT "ENTER BOOK'S ID";ID
   INPUT "ENTER BOOK'S NAME";BN$
   INPUT "ENTER AUTHOR'S NAME";AN$
   INPUT "ENTER PRICE";P
   INPUT "ENTER QUANTITY"; Q
   WRITE #1, ID,BN$,AN$,P,Q
   INPUT "DO YOU WANT TO CONTINUE?";CH$
   IF UCASE$(CH$)= "Y" THEN GOTO TOP
   CLOSE #1
   END

6. WAP to delete some records from “RECORD.dat” file where computer ask user to enter the record, which is to be deleted. (HINT : name, address, and telephone number).


Ans:-

CLS
OPEN “RECORD.DAT” FOR INPUT AS #1
OPEN “NEWRECORD.DAT” FOR OUTPUT AS #2
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#
END IF
WEND
CLOSE #1, #2
 KILL “RESULT.DAT”
NAME “NEWRESULT.DAT” AS “RESULT.DAT”
END

7. Write a program to open a data file “SALARY.DAT” that contains Name, Address, Date of birth, and Telephone number of some employees. Display all those records whose date of birth is in current month.

Ans :-


CLS
OPEN “SALARY.DAT” FOR INPUT AS #1
WHILE NOT EOF(1)
INPUT #1, N$, A$, D$, , T
B$=LEFT$(D$, 2)
C=VAL(B$)
E$=LEFT$(DATE$, 2)
F=VAL(E$)
IF C=F THEN
PRINT N$, A$, D$, E, T
WEND
CLOSE #1
END

8. Write a program to delete some records from datafile "RECORD.DAT" which contains Name , Class, Roll Number of students :-


Ans :-


CLS
OPEN " RECORD.DAT" FOR INPUT AS #1
OPEN"NEWRECORD.DAT" FOR OUTPUT AS #2
A:
DO WHILE NOT  EOF(1)
INPUT #1,N$,C,R,S$
PRINT "NAME "; N$
PRINT "CLASS";C
PRINT "ROLL NUMBER ";R
PRINT "SECTION";S
INPUT "DO YOU WANT TO DELETE THIS RECORD (Y/N)  ";Y$
IF Y$  = "Y" THEN GO TO A:
ELSE
WRITE #2,N$,C,R,S$
END IF
LOOP
CLOSE #1
CLOSE #2
KILL "RECORD.DAT"
NAME " NEWRECORD.DAT" AS "RECORD.DAT"
END

9. Write a program to read entire data of the sequential data file "RECORD.DAT" which contains Name , Address , and Phone Number of the employees then display all the records as well as count total number of records stored in the file :-


Ans:-

CLS
OPEN " RECORD.DAT" FOR INPUT AS #1
P = 0
PRINT "NAME ","ADDRESS","PHONE"
WHILE NOT EOF (1)
INPUT #1,N$,A$,P
PRINT "NAME";N$
PRINT"ADDRESS";A$
PRINT "PHONE NUMBER ";P
P = P + 1
WEND
PRINT " TOTAL NUMBERS OF RECORDS ";P
CLOSE #1
END

10. Write a sequential datafile to insert user need record to an existing data file "RECORD.DAT" including Name , Age , Address , and Phone number :-


Ans:-


CLS
OPEN "RECORD.DAT" FOR APPEND AS #1
Do
INPUT " NAME "; N$
INPUT "AGE";A
INPUT"PHONE NUMBER";P
WRITE #1,N$,A,A$,P
INPUT "ANY MORE(Y/N) :-;Y$
LOOP WHILE Y$="Y"
CLOSE #1
END

11 Write a program to create a data file “RECORD.dat” to store Name,Class,and marks in three subjects of 15 students :-


Ans :-


CLS
OPEN “RECORD.DAT” FOR OUTPUT AS #1
FOR I = 1 TO 15
CLS
INPUT “Enter name”; N$
INPUT “Enter class”; C
INPUT “Enter marks in three different subjects”; M1, M2, M3
WRITE #1, N$,C,M1,M2,M3
NEXT I
CLOSE #1
END

12.A data file named “INFO.dat” contains Name of Staff,, Post and Basic Salary. Write a program to count and display total number of records in a file :-


Ans :


CLS
OPEN “INFO.DAT” FOR INPUT AS #1
C=0
WHILE NOT EOF(1)
INPUT #1, N$, D$, P$, S
C=C+1
WEND
PRINT “Total number of records=”; C
CLOSE #1
END

Friday, December 27, 2019

13. Write a program to create a sequential data file “RESULT.DAT” to store name of students , class , section and roll number according to the user need :-

Soln.

CLS
OPEN “RESULT.DAT” FOR OUTPUT AS #1
DO
INPUT “Enter name”; N$
INPUT “Enter class ”; C
INPUT “Enter section ”; S$
INPUT "Enter roll no. " N
WRITE #1, N$, C,S$,N
INPUT” Do you want to add more records (Y/N)”;CH$
LOOP WHILE  CH$=”Y”
CLOSE #1
END

14.A sequential data file “SALARY.DAT” contains the information such as Name of Employee, Post, and Basic-Salary. Write a program to display those records whose Basic-salary is in between 15000 to 20000


Ans :

CLS
OPEN “SALARY.DAT” FOR INPUT AS #1
WHILE NOT EOF(1)
INPUT #1, N$, P$, S
IF B$>=10000 AND B$<=15000 ” THEN
PRINT N$, P$, S
END IF
WEND
CLOSE #1
END

15.Create a sequential data file “SALARY.DAT” to store name , address, and salary of the workers in a bank : -

Ans:

CLS
OPEN “SALARY.DAT” FOR OUTPUT AS #1
DO
INPUT “Enter worker name”; CN$
INPUT “Enter address”; A$
INPUT “Enter salary ”;S
WRITE #1, CN$, A$, S
INPUT “Do you want to add more records (Y/N)”; Ans$
LOOP WHILE Ans= “Y”
CLOSE #1
END



For Suggestion / Help / Feedback :-
Kindly Contact :
E-mail : Shahadit65@gmail.com
Phone No. : 9865416703


Thank - You 


             

File Handling

Introduction:- Data are used in all the programs for retrieving information. In QBASIC you can store data in a separate file called a da...