## File Handling

Subject: Computer Science

#### Overview

File Handling is a process to create data file,write data to the data file and read data from it.This note provides an information about file handling in QBASIC.

#### 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

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.

NPUT#: 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 end a 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.
##### Things to remember
• File Handling is a process to create data file,write data to the data file and read data from it.
• There are two types of file handling. They are Sequential Data File and Random Data File.
• Files, shell, name, kill etc. are some useful commands with their function and syntax.
• It includes every relationship which established among the people.
• There can be more than one community in a society. Community smaller than society.
• It is a network of social relationships which cannot see or touched.
• common interests and common objectives are not necessary for society.
##### Saving Preferences

A file that contains a set of instructions that are needed for data processing is known as program and file that contains only data required during data processing is known as data file.

A sequential access data file 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.
OPEN"Library.dat" FOR OUTPUT AS #1
CLS
INPUT"Enter book number";B
INPUT"Enter book name";A$WRITE #1,B,A$
CLOSE #1
END

OPEN"STD.dat" FOR OUTPUT AS #1
CLS
TOP:
INPUT" Enter your name";N$INPUT"Enter your address";Ad$
WRITE #1,N$,Ad$,T
INPUT"Do you want to add more records (Y/N)";A$IF A$="Y" THEN GOTO TOP:
CLOSE #1
END

OPEN"Std.dat" FOR INPUT AS #1
CLS
DO WHILE NOT EOF(1)
INPUT#1,N$,C,R, PRINT N$,C,R
LOOP
CLOSE#1
END
OPEN"student.dat"FOR APPEND AS #1
CLS
TOP:
INPUT"Enter your name";N$INPUT" Enter your marks in English";E INPUT"Enter your marks in Nepali";N INPUT"Enter your marks in Computer";C WRITE#1,N$,E,N,C
INPUT"To add more records (Y/N)";A$IF A$="Y" THEN GOTO TOP:
CLOSE #1
END
OPEN" Employee.dat" FOR INPUT AS #1
CLS
DO WHILE NOT EOF ( 1)
INPUT #1,N$,D$,P$,S If S>= 5000 AND S<= 1000 THEN PRINT N$,D$,P$,S
END IF
CLOSE#1
END