File Handling in QASIC

 Learning outcomes

  • Data file and its modes of opening
  • CURD operation in file
  • statements in file handling
Introduction
file handing is a process of creating a data file, write data and reading a data.
there are 2 types of file in qbasic
  1. Program file 
  2. Data file
Program File is a set of instruction written in a computer language for performing certain task. extension of Qbasic program .bas

Data file collection of data such as name, marks ,practical marks total marks etc. In qbasic we generally create  and update txt and dat file.

Advantage of data file:
  • Data stored can be reused
  • we can data file from different program 
  • it helps to store output for future use
  • Data file can created ,modify and delete

Modes of sequential access
propose of opening data file mode.
3 modes modes of opening data files are 
  1. output mode
  2. Input mode
  3. Append mode

Modes

Purpose

Output or ‘O’

It is used to create a new sequential data file and read data in it. Data is written from beginning Of the File(BOF) in this mode

Input or ‘I’

It is used to read/display data or information from the existing data file

Addend or ‘A’

It is used to add more record in the existing data file at the End of File(EOF)











Statement of file handling

OPEN Statement
is used to open sequential file for writing data to data file or reading data from data file using Input , output and append.

syntax:
OPEN "filename" FOR <FILE_MODE> AS #FILE_NUMBER
OR
OPEN "file mode",#file number,"file name"
Where file name is name of file that need to open. It contains upto 8 character with extension. It can also include path and drive specifications.

Eg.
OPEN "emp.dat" for output as #1
OR
OPEN "I" , #1,"emp.dat"

Write statement
write statement is used to send or store one or more data items to specified file using either output or append mode. When we use #WRITE statement to store data in data file , data are separated by commas and string data are kept in the double quotation marks.
Syntax:
Write # [file number], [expression list]
where,
file number is an integer used in OPEN statement for the file
expression list is one or more string or numeric expression separated by commas
example
WRITE #1, sn, n$, a$, p

PRINT Statement
Print statement is used to write data in specified sequential data file using either output or append mode. It stores data. when you use PRINT statement to store data in data file, data are separated by spaces and string data are not kept in the double quotation marks
syntax:
PRINT #[file number], [expression list]
where,
file number is an integer used in OPEN statement for the file
expression list is one or more string or numeric expression separated by commas

example:
PRINT #1,name$,add$,phone

Reading data from a File
INPUT statement
the input statement is used to read data of the existing sequential data file and store them in variables. It reads data of the file in the same order as they are stored.
syntax:
INPUT # [file number], [variable list]
where,
file number is the number o an open sequential file
variable list is one or more simple variable names, separated by commas that will be assigned values from the file. Make sure the data type of each variable in the list matches the data type of the incoming data item

example:
INPUT #1, sn,n$,a$,p

LINE INPUT# STATEMENT
The line input statement reads the entire line or maximum 255 characters rom the keyboard or sequential file. It accepts special characters such as commas, quotes, slashes and other which the INPUT statement doesn't allow and store data only in a string variable
syntax:
LINE INPUT #[file number], [variable$]

example:
LINE INPUT # 2, a$

Closing files(s)
CLOSE and CLOSE# Statement
the close statement is used to close one or more opened data file
the CLOSE# statement is used to close only specific file
syntax:
CLOSE[# FILENUMBER....]
where 
if all arguments are omitted all open files are closed 
file number is the number of an open file

example:
close #1

EOF() function
The EOF() function is used to check the end of the file marker while reading the data from the open sequential file. It returns TRUE if the end of the file has been reached and returns FALSE when the end of the file has not been reached 
syntax:
EOF [file number]

BOF() function
The BOF function is used to check the beginning of a file. It returns true if the beginning of the file has been reached
syntax:
BOF [file number]
 
NAME statement
name statement is used to rem\name a file
syntax 
name "old filename" as "new filename":
example 
name "emp.dat" as "employee.dat"
 
files statement
file statement displays a list of ilses name of a current dist or a directory
syntax
files [filespec]
where 
if all argument are omitted  all files in the current directory are listed
filespec is string expression or constant that includes filename and device name

eg
files "*.bas"
list of all file having .bas extention from current drive

Comments