Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Thursday, February 17, 2022

File Handling in QBASIC

 

File Handling in QBASIC


We use computers to handle large volume of data and we can use any storage device to store that data. The data is stored in these devices using the concept of files. A file is a collection of related data stored in a particular area on the disk. Program can be designed to perform the read and write operations on files. Every data that you perform in a program disappears due to the volatile nature of RAM. In the program if you want to store the data permanently, you must use the file concept.

However, when the files are stored on the computer, the files have to be kept in such a way that the records can be located, processed, and selected easily by the computer program. The handling of files depends on both input and output requirements, and the amount of data to be stored.

A file is a collection of data or information or instructions. Files are stored permanently in the secondary storage devices using a unique name called filename. Files can be classified into two types.

i. Program file

ii. Data file

 

i. Program File

A program is a collection of instructions written in a computer language. After writing the program in the computer, it can be stored in the secondary storage devices as a file, which is called program file. In previous lesson, we have written programs and stored them in hard disk for future, which are called program files.

ii. Data File

Data files are the files of computer in which data are permanently stored. So that we can add, modify and delete according to the requirements.

There are two types of data files which are created in QBASIC.

a) Random Access data file: In this type of file, data can be read and modified randomly. In this type, if we want to read the last record of a file, we can read it directly.

 

b) Sequential File: It is a file that should be accessed in a sequential manner starting at the beginning of the data block and proceeding in order until an end-of-data marker is met of the desired number of items has been read.

 For example: if we have to access the 50th records from the file “avn.dat”, sequential file first read 49th records, then only it can go to the 50th record.

 

 Creating a sequential Data File:

Following are the steps for creating/writing data into a sequential data file

i) Open the data file (Master file)

ii) Get information from the user.

iii) Write the data items to the file.

iv) Repeat this step until all the data is written in master file.

v) Close the file.

 

Opening or Creating a file:

Open statement:

It opens a sequential file or devices.

Syntax:

OPEN “file name”  [FOR Mode] AS [#]file number

OR

OPEN “Mode”, [#]file number, “file name”

Where,

File name: It is name of the file which you want to open or create.

FOR: It specifies the particular operation that the program will execute with the open file.

MODE: One of the modes: INPUT, OUTPUT, APPEND

File number: A number in the rage 1 to 255 that identifies the file while it is open

Example:

OPEN “school.dat” FOR OUTPUT AS #5

This example opens “school.dat” file in OUTPUT(writing) mode.

           

OPEN “1”, #1, “school.dat”

This example opens “school.dat” in INPUT mode.

 

File Modes and Descriptions

File Mode

Descriptions

OUTPUT or “O”

Open or Create a new file to store/write data. Its positions the file pointer to the beginning of the file.

INPUT or “I”

Open a file sequential reading mode.

APPEND or “A”

Opens an existing data file to add new data. It positions the file pointer to the end of file (EOF).

 

 Difference between OUTPUT and APPEND mode

OUTPUT mode

APPEND mode

It creates a new file and prepares to write data to the file.

It opens already saved file to add few new records in the file.

If the file already exists its current contents will be destroyed.

If the specified file does not exist APPEND mode creates it.

 

Input information from the user:

INPUT statement is used to get the data from the user.

Syntax:

INPUT [;] [“prompt”;|,] variable list

Where,

INPUT is a statement to get the data.

Prompt is a string constant, enclosed in quotation marks that will be displayed when the statement is executed.

Vatriable list is the name of a numeric or string variable, or array element, to which the incoming data item will be assigned.

Example:

INPUT “Enter any three numbers”;a,b,c

 

Writing or Storing Data into the Sequential Data file ( using OUTPUT Mode)

WRITE # or PRINT # statements are used to store data in data file. 

WRITE# is used to store the data without leaving space.

PRINT # is used to store the data by leaving the space.

Syntax:

WRITE #filenum, expression_list

OR

PRINT #filenum, expression_list

Where,

Filenum is the number used when the file was opened for OUTPUT.

Expression_list consists of the numeric or string expression to be written to the file.

Example:

WRITE #1, n$,a$

OR

PRINT #1, n$,a$

 

Difference between WRITE # and PRINT #

PRINT#

WRITE#

It adds spaces between data items while storing data.

Commas are inserted between the data.

Double quotation marks is not there in PRINT # statement


Double quotation marks are there in WRITE# statement.


It occupies more space in disk

It occupies less space in disk.

 

 

PRINT # USING:

This command is used to writes formatted output  for a file on the screen.

 

LINE INPUT #

It reads the entire line or maximum 255 characters form the keyboard or the sequential file.

 

Closing a Data File:

CLOSE statement is used to close a data file after completing the writing or reading process.

Syntax:

CLOSE # file number

Where,

[#] filenumber is the file number assigned to the file in an OPEN statement.

Example:

CLOSE #5      à It close the file number #5

CLOSE #3, #1à It close first file number #3 and then file number #1

CLOSE           à It closes all opened files.

 

Solved Example: 1

WAP to create a data file (avn.dat) and store name, class and address. And also ask the user to continue.

 

Note:

i) In above program, new data file is created and input data are stored in data file “avn.dat”. 

ii) If the “avn.dat” file is already then it overwrites on existing data file because of OUTPUT mode.

iii) To see whether contents are stored in data file or not, go to DOS prompt and type the command with file name in DOS prompt. Example: C:\> TYPE  avn.dat [enter]

 

Reading Data from a Sequential Data file (INPUT Mode)

To read the data from the sequential data file we have to open a data file in INPUT mode.

Following steps are used for reading data from a sequential data file.

i) Open the sequential data file in INPUT mode.

ii) Test the end of file condition by using EOF statement.

iii) Read the data from the file.

iv) Display the data on the screen.

v) Close the file after reading process is completed.

 

Open the Sequential Data File

This step is same as the step for opening a file for creation. INPUT mode is used to retrieve data from the sequential data file.

Syntax:

OPEN “file name”  [FOR Mode] AS [#]file number

OR

OPEN “Mode”, [#]file number, “file name”

Example:

OPEN “avn.dat” FOR INPUT AS #1

OR

OPEN “I”,#1, “avn.dat”

 

Read the Data from the Sequential Data File:

To read the data from the sequential data file INPUT # statement is used.

Syntax:

INPUT # filenum, variable_list

Where,

filenum is the number under which the file was opened for INPUT

variable-list It contains variable names which are assign in the data file.

Example:

INPUT #1, n$,a,b,c

Note:

The variable type must match that was used while storing data into data file. Or Variable type of OUTPUT and INPUT mode must be same.

 

Close the data file:

Use CLOSE statement after data is completely read from the data file.

Syntax:                     

CLOSE [#]filenum]

Example:      CLOSE #1

 

 Getting End of File (EOF)

The EOF function tests for the end of a file. It retunes -1 if End-Of-File has been reached. The function returns a 0 if End-of-File has not been reached.

Syntax:

EOF(filenumber)

Where,

Filenumber is the number that was used to open the file.

Example:      EOF(1)

 

Solved Example: 2

WAP to print all the records on the screen of data file (avn.dat), which has contents student name, class and address.

 


 Note:

In above program “avn.dat” data file is opened in INPUT mode and all the data are read and display on the screen.

Adding Data to a Sequential Data File (APPEND Mode)

APPEND mode is used to add the new data in existing sequential data file. Following steps are used to add more data in a sequential data file.

i) Open you master file in APPEND mode.

ii) Get data from the user.

iii) Write data in you master file.

iv) Repeat this step until all the data is written in you master file.

v) Close the master file.

 

Syntax:

OPEN “filename” FOR APPEND AS # filenumber

OR

OPEN “A”, #filenumber, filename

 

Example:

OPEN “avn.dat” FOR APPEND AS #1

OR

OPEN “A” #1, “avn.dat”    

 

Solved Example: 3

WAP to add some new records in the existing data file "avn.dat", which has contents name, address, and class.

CLS

OPEN "avn.dat" FOR APPEND AS #1

top:

INPUT "Enter name:-";n$

INPUT "Enter class:-";c

INPUT "Enter address:-";a$

WRITE #1, n$,c,a$

INPUT " Do you want to add more records (Y/N):";ans$

IF ans$="Y" or ans$="y" THEN GOTO top

CLOSE #1

END

 

LINE INPUT statement

It reads a entire line of up to 255 characters from the data file.

Syntax:

LINE INPUT #filenumber, string variables

Where,

Filenumber is the number under which the file was opened.

String variables holds a line or characters entered from the keyboard or read from a file.

 

Solved Example: 4

OPEN “avn.dat” FOR INPUT AS #1

DO WHILE NOT EOF(1)

LINE INPUT #1, b$

PRINT b$

LOOP

CLOSE #1

END

 

File Management Commands:

Following are the some important File commands used in File Handling.

a) CHDIR:

Function: This command is used to change the current directory

Syntax:     CHDIR <path>

Example:  CHDIR "C:\program\qbasic"

 

b) MKDIR:

Function:  This command creates a specified sub-directory inside the    current directory.

Syntax:     MKDIR <directory name>

Example:  MKDIR "A:\qbasic"

 

c) RMDIR:

Function: This command is used to remove or delete only sub-directory from a disk. It can remove only empty sub- directory.

Syntax: RMDIR <path>

Example:  RMDIR "C"\program\qb"

 

d) FILES:

Function: This command display the files on the disk. The directory and sub-directory also displayed on the screen.

Syntax:     FILES <file specification>

Example:  FILES "D:\qbasic\*.txt"

 

e) NAME:

Function: This command is used to change the name of a disk file  or directory.

Syntax: NAME <old file name> as <new file name>

Example: NAME "avn.bas" as  "aishwarya.bas"

 

f) SHELL:

Function: This command is used to enter the DOS prompt temporarily.

Syntax:     SHELL <command string>

Example:  SHELL

Note: To return back in QBASIC screen, type EXIT and press enter key.

 

g) KILL:

Function:  To delete file from specified location

Syntax: KILL "file name"

Example: KILL "xyz.dat"  

or               

KILL "D:\QBASIC\school.txt"

 

h) SYSTEM:

Function:  To quit from the QBASIC permanently.

Syntax:     SYSTEM

Example:  Type SYSTEM in the immediate Window and Press Enter key.

 

Updating Sequential Data File

We can update Sequential data file by adding, editing, searching and deleting data from the master file.

 

Modifying Data in a Sequential Data File

We can change the data from the existing records from the sequential data file. Use the following steps of that:

i) Open Master file in INPUT mode

ii) Open temporary data file in OUTPUT mode.

iii) Read the data from the master file and display on the screen.

iv) Write/store modified data into temporary file.

vi) Repeat these steps until all the data is written from the master file to temporary file.

Close both files.

vii) Delete the master file using KILL statement.

viii) Change the temporary file name into previous master file name using NAME statement.

 

Solved Example: 5

CLS

REM “This program is used for modifying/edit data file”

OPEN "avn.dat" FOR INPUT AS #1

OPEN "temp.dat" FOR OUTPUT AS #2

WHILE NOT EOF(1)

INPUT #1, n$, ad$, ag

PRINT "Nmae"; n$

PRINT "Address"; ad$

PRINT "Age"; ag

PRINT :

INPUT "Do you want to modify age"; yes$

IF UCASE$(yes$) = "Y" THEN

INPUT "Enter new age="; na

WRITE #2, n$, ad$, na

ELSE

WRITE #2, n$, ad$, na

END IF

WEND

CLOSE #1, #2

KILL "avn.dat"

NAME "temp.dat" AS "avn.dat"

END

 

 Deleting/removing Records from the Sequential Data File

From data file we can remove unwanted records. 

i) Open Master file in INPUT mode.

ii) Open temporary file in OUTPUT mode.

iii) Read the data from master file and display on the screen.

iv) Display the confirmation message on the screen for deleting records.

v) Keep the record in master file which you want to delete. Write data in temporary file which you don’t want to delete.

vi) Repeat these steps until end of the file.

vii) Close the files.

viii)Delete the master file.

ix) Rename temporary file as master file.

 

Solved Example: 6

REM "Delete data from sequential data file"

CLS

OPEN "avn.dat" FOR INPUT AS #1

OPEN "temp.dat" FOR OUTPUT AS #2

abc:

DO WHILE NOT EOF(1)

INPUT #1, n$, ad$, ag

PRINT "Name"; n$

PRINT "Address"; ad$

PRINT "Age"; ag

INPUT " Do you want to delete record"; ans$

IF UCASE$(ans$) = "Y" THEN

GOTO abc

ELSE

WRITE #2, n$, ad$, ag

END IF

LOOP

CLOSE #1, #2

KILL "avn.dat"

NAME "temp.dat" AS "avn.dat"

END

 

 Inserting New records:

We can add new records in already stored data file.

i) Open the Master file in INPUT mode.

ii) Open temporary file in OUTPUT mode.

iii) Read the data from the main file and store/write to temporary file until the you not added new data.

iv) Write another data into temporary data file.

v) Repeat these steps until the end of file.

vi) Close both of these files.

vii) Erase the main master file.

viii) Change the temporary data file name into master file name.

 

 Solved Example: 7

REM “Program to inserting new records”

OPEN "avn.dat" FOR INPUT AS #1

OPEN "temp.dat" FOR OUTPUT AS #2

DO WHILE NOT EOF(1)

INPUT #1, n$, ad$, ag

PRINT "Name"; n$

PRINT "Address:"; ad$

PRINT "Age"; ag

INPUT "Do you want add records before this"; ans$

IF UCASE$(ans$) = "Y" THEN

INPUT "Enter name="; name$

INPUT "Enter address="; add$

INPUT "enter age"; age

WRITE #2, name$, add$, age

END IF

LOOP

WRITE #2, n$, ad$, ag

CLOSE #1, #2

KILL "avn.dat"

NAME "temp.dat" AS "avn.dat"

END

 

Searching a records from  Data File:

Specific records can be search from the data file.

 Example: 8

REM “program for searching the specific record”

OPEN "avn.dat" FOR INPUT AS #1

CLS

INPUT "Enter your name to search"; name$

flag = 0

DO

INPUT #1, n$, ad$, ag

IF name$ = n$ THEN

PRINT n$, ad$, ag

flag = 1

EXIT DO

END IF

LOOP WHILE NOT EOF(1)

IF flag = 0 THEN PRINT "Record not found"

CLOSE #1

END

 

Also Read: Programming structures or logic in QBASIC


Copying records or making duplicate data file

Solved Example: 9

REM “Program for copying original file to temporary file”

CLS

OPEN "avn.dat" FOR INPUT AS #1

OPEN "temp.dat" FOR OUTPUT AS #2

DO WHILE NOT EOF(1)

INPUT #1, n$, ad$, ag

WRITE #2, n$, ad$, ag

LOOP

CLOSE #1, #2

END

 

You may Read:

QBASIC program to display the Perfect Square numbers from 1 to 50.



C program to display all factors of the input number in C programming.

No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages