(X) Ans of Ch-13 File Handling

Chapter-13 File Handling 1. Fill in the banks: (a) A program is a collection of instructions written in a computer language. (b) The files of computer are classified into program file and data file. (c) QBASIC supports two types of data file. They are Random Access data file and Sequential Access data file. (d) Different modes of sequential data file are INPUT, OUTPUT and APPEND. (e) WRITE statement is used to write data to the opened data file. (f) INPUT statement is used to read data from the opened data file. (g) Append mode allows to add records to the existing data file. 2. State whether the following statements are true or false: (a) A data file is a collection of programs. FALSE (b) A program can be used to create a data file. TRUE (c) Only one program can use a data file. FALSE (d) A data file can be created,modified or deleted. TRUE (e) In a sequential access data file, data can be read from any position. FALSE (f) Data can be added or appended in a sequential data file. TRUE (g) Data can be copied from one data file to another. TRUE (h) Output mode is used to create new data file. TRUE 3. Write the syntax and purpose of the following statements and functions: (a) OPEN Syntax: OPEN “Filename.Extension” FOR AS #File_number Purpose: OPEN statements opens a sequential for only one purpose : either reading or writing. (b) CLOSE Syntax: CLOSE [#File_number,] [#File_number] Where, #File_number of an open file. Purpose: CLOSE statement is used to close the data file. (c) WRITE # Syntax: WRITE [#File_number,] Expression List Purpose: WRITE statement sends one or more data items to the specified file. (d) INPUT # Syntax: INPUT #File_number, Variable List Where, #File_number The number of an open file. Variable List One or more variables separated by a comma. Purpose: INPUT# statement is used to extract the data items from a data file for reading purpose. (e) INPUT$( ) Syntax: INPUT$ ( n , [#file_number]) Where, n The number of characters (bytes) to read. #file_number The number of an open file. Purpose: INPUT$( ) reads the specified number of character from the data file. (f) EOF ( ) Syntax: EOF(#File_number) Where, #File_number Number of the open file that is needed to be read. Purpose: EOF( ) is used to detect the End of the file. (g) KILL Syntax: KILL “filename” Purpose: KILL statement is used to delete the file or files. (h) NAME Syntax: NAME “oldname” AS “Newname” Purpose: NAME statement is used to rename the data file. (i) FILES Syntax: FILES [Filespace$] Where, Filespace$ may include filenames or path. Purpose: The FILE statement displays the files of the current subdirectory or specified subdirectory. (j) MKDIR Syntax: MKDIR Path, Where, Path is the sequence of directory and subdirectories. Purpose: MKDIR is used to creates a subdirectory which is used to manage them. (k) CHDIR Syntax: CHDIR Purpose: The CHDIR statement allows QBASIC to change from one directory to another. (l) RMDIR Syntax: RMDIR Path Where, Path is the sequence of directory and subdirectories. Purpose: RMDIR statement is used to remove or delete only subdirectory from a disk. (m) SHELL Syntax: SHELL [DOS command] Purpose: This command allows to exit from QBASIC to DOS prompt temporarily or to execute DOS commadns from QBASIC. (n) LINE INPUT# Syntax: LINE INPUT [#file number] ["prompt";] variable$ Purpose : The LINE INPUT statement reads the entire line or maximum 255 characters from the keyboard or from the sequential file up to a carriage return. 4. Write algorithms for the following statements: (a) To read name,address and salary of 10 different persons from the keyboard and store them in a data file. Algorithm: Step 1: Open the data file to read the data (Input mode) Step 2: Set I = 1 Step 3: Read the data from the data file (Name, Address and Salary) Step 4: Display the data on the screen Step 5: Increment I by 1 i.e. I=I+1 Step 6: Check the I. Is I<=10. If Yes: Repeat Step 3,4,5,6 If No: Go to step 7. Step 7: Close the data file Step 8: Stop (b) To read all data (Name, Address and Salary ) from the data file and display them on the screen. Algorithm: Step 1: Open the data file to read the data (Input mode) Step 2: Read the data from the data file (Name, Address and Salary) Step 3: Display the data on the screen Step 4: Close the data file Step 5: Stop 5. Answer the following questions: a. Define a file. Differentiate between a data file and a program file. ANSWER: A Collection of data or information or instructions which are stored permanently in the secondary storage device using a unique filename is called file.
Data File Program File
1. A collection of the related data stored in the hard disk or any other secondary storage devices separately from the program that uses it. 1. A file which contains instructions, which are used to manipulate data and give the output and stored in the secondary storage devices is called program file.
b. What are sequential access data file and random access data file? ANSWER: Sequential Access Data File: A sequential data file conducts reading and writing operation sequentially, from the beginning of the file to the end. For example, if you store 10 different data and if you want read , 9th data, then you will have to pass through the 1st to 8th data to search the 9th data; there is no way to directly go to it. Random Access Data File: A random data file conducts reading and writing operations randomly. It is suitable for the database that requires direct access to individual records stored on the disk. For example, to read 100th data item, one does not need to go through all the data from 1 to 99. Data number 100 can be directly accessed without going through other data items. c. What are the advantages of creating a data file? ANSWER: The advantages of creating data file are as follows: Once a data file is created , it can be reused. Different programs can share the same data file. It can be created, modified or deleted. A data file can store a large output for the future use. d. What are the three different operations of a data file? Explain them. ANSWER: The three modes of operations of data file are as follows:
Mode of File Function
Output ModeFor writing data or information to a new file
Input Mode For reading data or information from a data file
Append Mode Appending information or data to the end of an existing file
e. Differentiate between INPUT # and LINE INPUT # statement. ANSWER: INPUT # Syntax: INPUT #File_number, Variable List Where, #File_number The number of an open file. Variable List One or more variables separated by a comma. Purpose: INPUT# statement is used to extract the data items from a data file for reading purpose. LINE INPUT # Syntax: LINE INPUT [#file number] [“Prompt”] ;Variable$ Where, Prompt It is an optional string of characters enclosed in quotes to prompt for input. Variable$ String variable to which LINE INPUT assigns all characters. Purpose: LINE INPUT is used to store data only is the string variable and can reads the entire line or maximum 255 characters from the keyboard or from the sequential file up to a carriage return. f. What is the use of the “file number” of the data file? Write the range of the file number. ANSWER: The file number is used in a data file to identify the each file with a unique number . When a file is open, its unique file number identifies it. File number may be integer number from 1 to 255. g. Differentiate between input mode and output mode. ANSWER:
Input ModeOutput Mode
1. Open a file in reading mode.1. Open a new file to write data.
2. It never creates a new data file. 2. It always creates a new data file.
3. Old records will not be deleted in this mode. 3. Old records will be deleted in this mode.
h. Differentiate between output mode and append mode. ANSWER:
Output ModeAppend Mode
1. It always creates a new data file.1. If the specified file does not exist , APPEND creates it.
2. Old records will be deleted in this mode.2. Old records will not be deleted in this mode.
6. Debug the following programs and test them in the computer. a. REM to add or append data OPEN “C:\SAL,DAT”FOR OUTPUT AS #1 DO CLS INPUT”ENTER THE NAME::::::::”;N$ INPUT”ENTER THE POST::::::::::”;P$ INPUT”ENTER SALARY”;S WRITE # 2, N$, P$, S INPUT ”Want To Supply More Y/N::”;CH LOOP WHILE UCASE$(CH) = ”Y” CLOSE #2 END b. REM to copy data from”SAL.DAT” to “TEMP.DAT” OPEN “SAL.DAT” FOR INPUT AS # 1 OPEN”TEMP.DAT” FOR OUTPUT AS #2 CLS DO INPUT #2, N$, P$, S WRITE #2, N$, P$, S LOOP WHILE NOT EOF( ) CLOSE #2, #1 END 7. Type the following programs and see the outputs. a. REM to create data file DECLARE SUB ADDREC( ) CALL ADDREC END SUB ADDREC CLS OPEN”STAFF.DAT” FPR OUTPUT AS #1 DO INPUT ”Enter Name:”;N$ INPUT” Enter address;”;A$ INPUT “Enter Salary”;S WRITE #1, N$, A$, S INPUT”Wants to Continue Y/N”;C$ C$= UCASE$(C$) LOOP WHILE C$=”Y” CLOSE #1 END SUB b. REM to read and display the data DECLARE SUB DISPREC ( ) CALL DISPREC END SUB DISPREC CLS PRINT ”NAME”,”ADDRESS”,”SALARY” OPEN “STAFD.DAT” FOR INPUT AS #1 DO INPUT #1, N$, AD$, SAL PRINT N$, AD$, SAL LOOP WHILE NOT EOF (1) CLOSE #1 END SUB c. REM to display selected data DECLARE SUB DISPREC ( ) CALL DISPREC END SUB DISPREC CLS PRINT ”NAME”,”ADDRESS”,”SALARY” OPEN “STAFF.DAT” FPR INPUT AS #1 DO INPUT #1, N$, AD$, SAL IF SAL>= 35000 THEN PRINT N$, AD$, SAL END IF LOOP WHILE NOT EOF ( ) CLOSE #1 END SUB d. REM to copy data from one data file to another. DECLARE SUB DATACOPY ( ) CALL DATACOPY END SUB DATACOPY CLS OPEN”SATFF.DAT” FOR INPUT AS #1 OPEN”TEMPT.DAT” FOR OUTPUT AS #2 DO INPUT#1, N$, AD$, SAL IF UCASE$(AD$) = “KATHMANDU” THEN WRITE #2, N$, AD$, SAL END IF LOOP WHILE NOT EOF ( ) CLOSE #1, #2 END SUB e. REM to count records of data file DECLARE SUB DISPREC ( ) CALL DISPREC END SUB DISPREC CLS OPEN “STAFF.DAT” FOR INPUT AS #1 C=1 DO LINE INPUT #1, N$ C=C+1 LOOP WHILE NOT EOF (1) CLOSE #1 PRINT ”Total Records=”;C END SUB 8. Write the programs for the following:
  1. To create a data file that stores name, address and salary for at least ten different persons.

ANSWER:

OPEN "EMPLOYEE.TXT" FOR OUTPUT AS #1

FOR P = 1 TO 10

INPUT "ENTER NAME,ADDRESS AND SALARY", NAME$, ADDRESS$, SALARY

WRITE #1, NAME$, ADDRESS$, SALARY

NEXT P

CLOSE #1

END


  1. Write a program to store name, roll no and marks for three subjects in a sequential data file named “STUDENT.DAT”. The program should terminate according to the user’s choice.

ANSWER:

OPEN "STUDENT.DAT" FOR OUTPUT AS #1

DO

INPUT “Enter name,roll and marks in 3 subjects ” , NAME$, ROLL, SUB1, SUB2, SUB3

WRITE #1, NAME$, ROLL, SUB1, SUB2, SUB3

INPUT “DO YOU WANT TO ADD SOME MORE RECORDS (Y/N) ” , CH$

LOOP WHILE UCASE$(CH$) =  “Y”

CLOSE #1

END

  1. Write a program to input data from the data file named “student.dat” which, contains name, roll no and marks for three subjects for n number of students. Calculate total and percentage and display the result on the screen.

ANSWER:

CLS

OPEN "STUDENT.DAT" FOR INPUT AS #1

PRINT "NAME", "ROLL", "SUB1", "SUB2", "SUB3", "TOTAL", "PERCENTAGE”

WHILE NOT EOF(1)

INPUT #1, NAME$, ROLL, SUB1, SUB2, SUB3

TOTAL = SUB1 + SUB2 + SUB3

PERCENTAGE = TOTAL / 3

PRINT NAME$, ROLL, SUB1, SUB2, SUB3, TOTAL, PERCENTAGE

WEND

CLOSE #1

END

  1. Write a program to open a data file named “STUDENT.DAT” and add some data on it. Fields of the data file are “Name”, “Address”, “Telephone”.

ANSWER:


OPEN “STUDENT.DAT” FOR APPEND AS #1

DO

INPUT “Enter name,address and telephone ”; NAME$, ADDRESS$, TELEPHONE$

WRITE #1, NAME$, ADDRESS$, TELEPHONE$

INPUT “DO YOU WANT TO ADD SOME MORE RECORDS”; CH$

LOOP WHILE UCASE$(CH$) = “Y”

CLOSE #1

END

  1. Write a program to ask item name, sold quantity, amount and salesman’s name and store them in a sequential data file “SALES.DAT”. Ask the user if he/she wants to enter another data or not.

ANSWER:


OPEN “SALES.DAT” FOR OUTPUT AS #1

DO

INPUT “Enter item name, sold quantity , amount , salesman’s name ” ; ITEMNAME$, SOLDQTY , AMOUNT ,SALES NAME$

WRITE #1, ITEMNAME$ , SOLDQTY , AMOUNT , SALES NAME$

INPUT “ DO YOU WANT TO ADD SOME MORE RECORDS ” ; CH$

LOOP WHILE UCASE$(CH$) = “Y”

CLOSE #1

END


  1. Write a program to read all the data of the data file named “SALES.DAT”, where fields are unknown. Display the result on the screen.

ANSWER:

OPEN “SALES.DAT” FOR INPUT AS #1

WHILE NOT EOF(1)

INPUT  #1, ITEMNAME$ , SOLDQTY , AMOUNT , SALES NAME$

PRINT  ITEMNAME$ , SOLDQTY , AMOUNT , SALES NAME$

WEND

CLOSE #1

END


  1. A data file named “STUDENT.DAT” contains a number of records. Write a program to display all records from records number 5 to 10.

ANSWER:

OPEN "STUDENT.TXT" FOR INPUT AS #1

WHILE NOT EOF(1)

INPUT #1, SN, NAME$

IF SN >= 5 AND SN <= 10 THEN

PRINT SN, NAME$

END IF

WEND

CLOSE #1

END

  1. Write a program to read all the data from the data file “SALES.DAT” and copy them to another data file named “TEMP.DAT”.

ANSWER:

OPEN "SALES.DAT" FOR INPUT AS #1

OPEN "TEMP.DAT" FOR OUTPUT AS #2

WHILE NOT EOF(1)

INPUT #1, ITEMNAME$, SOLDQTY, AMOUNT, SALESMANNAME$

WRITE #2, ITEMNAME$, SOLDQTY, AMOUNT, SALESMANNAME$

WEND

CLOSE #1, #2

END

  1. A data file contains names, marks of three subjects and total marks. Write a program to read the name from the keyboard and display information on the monitor. If the required data is located inside the data file, otherwise display, a message “DATA NOT FOUND”.

ANSWER:

OPEN “MARKS.DAT” FOR INPUT AS #1

INPUT “ ENTER THE NAME FOR SEARCH ”, NS$

FLAG = 0

DO

INPUT #1, NAME$ , SUB1 , SUB2 , SUB3

IF NAME$ = SN$ THEN

PRINT NAME$ , SUB1 , SUB2 , SUB3

FLAG=1

EXIT DO

END IF

LOOP WHILE NOT EOF(1)

IF FLAG=0 THEN PRINT “DATA NOT FOUND”

CLOSE #1

END

  1. A data file  contains the names of students , marks of three subjects and total marks of n number of students. Write a program to display the data for only those students whose total marks is greater than 100 but less than 150.

ANSWER:

OPEN “MARKS.DAT” FOR INPUT AS #1

WHILE NOT EOF(1)

INPUT #1, NAME$ , SUB1 , SUB2 , SUB3 , TOTAL

IF TOTAL > 100 AND TOTAL < 150 THEN

PRINT NAME$ , SUB1 , SUB2 , SUB3 , TOTAL

END IF

WEND

CLOSE #1

END


  1. Write a program to input the name of a student to delete his/her data from the data file named “STUDENT.DAT”. The data file contains name, roll no and marks for three different subjects.

ANSWER:

OPEN “MARKS.DAT” FOR INPUT AS #1

OPEN “TEMP.DAT” FOR OUTPUT AS#2

INPUT “ ENTER THE NAME TO BE DELETED ”, D$

DO

INPUT #1,N$,R,S1,S2,S3

IF N$<>D$THEN

WRITE #2,N$,R,S1,S2,S3

ELSE

PRINT “DELETED DATA”;N$,R,S1,S2,S3

END IF

LOOP WHILE NOT EOF(1)

CLOSE #1,#2

KILL “MARKS.DAT”

NAME “TEMP.DAT” AS “MARKS.DAT”

END







  1. A data file named “STUDENT.DAT” contains some records. Write a program to content total records of the data file.

ANSWER:

OPEN “ STUDENT.DAT” FOR INPUT AS #1

C = 0

DO

LINE INPUT #1, N$

C = C + 1

LOOP WHILE NOT EOF(1)

CLOSE #1

PRINT “TOTAL RECORDS” ; C

END

  1. A data file named “STUDENT.DAT” contains some records having fields name and marks in three subjects. Write a program to display all the records on the monitor if the marks in each subject are > = 60.

ANSWER:

OPEN “STUDENT.DAT” FOR INPUT AS #1

WHILE NOT EOF(1)

INPUT #1, NAME$ , SUB1 , SUB2 , SUB3

IF SUB1 >= 60 AND SUB2 >= 60 AND SUB3 >= 60 THEN

PRINT NAME$ , SUB1 , SUB2 , SUB3

END IF

WEND

CLOSE #1

END

  1. A data file named “OFFICE.DAT” contains some records having fields name, post and salary. Write a program to count the total number of officers in the office.

ANSWER:

OPEN “STUDENT.DAT” FOR INPUT AS #1

WHILE NOT EOF(1)

INPUT #1, NAME$ , POST$, SALARY

IF POST$ = “OFFICER” THEN

C = C + 1 

PRINT  NAME$ , POST$, SALARY

END IF

WEND

CLOSE #1

END


  1. Debug the following programs and test them in computer.

  1. REM to add or append

OPEN “C:\SAL.DAT” FOR OUTPUT AS #1

DO

CLS

INPUT “ENTER NAME” ; N$

INPUT “ENTER POST” ; P$

INPUT “ENTER SALARY” ; S

WRITE #2, N$ , P$, S

INPUT “ WANTS TO SUPPLY MORE Y/N” ; CH

LOOP WHILE UCASE$(CH)= “Y”

CLOSE #2,#1

END


Corrected Program


REM to add or append

OPEN “C:\SAL.DAT” FOR APPEND AS #1

DO

CLS

INPUT “ENTER NAME” ; N$

INPUT “ENTER POST” ; P$

INPUT “ENTER SALARY” ; S

WRITE #1, N$ , P$, S

INPUT “ WANTS TO SUPPLY MORE Y/N” ; CH$

LOOP WHILE UCASE$(CH$)= “Y”

CLOSE #1

END






  1. REM to copy data from “SAL.DAT” to “TEMP.DAT”

OPEN “SAL.DAT” FOR INPUT AS #1

OPEN “TEMP.DAT” FOR OUTPUT AS #2

CLS

DO

INPUT #2,N$,P$,S

WRITE # 2, N$ , P$, S

LOOP WHILE NOT EOF( )

CLOSE #2,#1

END

Corrected Program

REM to copy data from “SAL.DAT” to “TEMP.DAT”

OPEN “SAL.DAT” FOR INPUT AS #1

OPEN “TEMP.DAT” FOR OUTPUT AS #2

CLS

DO

INPUT #1 , N$ , P$ , S

WRITE # 2 , N$ , P$ , S

LOOP WHILE NOT EOF( 1 )

CLOSE #1 , #2

END


***** END OF CHAPTER - 13  *****