0% found this document useful (0 votes)
8 views5 pages

QBASIC File Handling Guide

The document provides an overview of file handling in QBASIC, detailing the types of files (program and data files) and the modes of opening sequential data files (output, input, and append). It includes examples of writing to and reading from files, as well as various programming tasks (WAPs) that demonstrate how to create, read, and append records in data files. Additionally, it explains the EOF function and provides sample code for managing employee and student records.

Uploaded by

rauniyarchanda01
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

QBASIC File Handling Guide

The document provides an overview of file handling in QBASIC, detailing the types of files (program and data files) and the modes of opening sequential data files (output, input, and append). It includes examples of writing to and reading from files, as well as various programming tasks (WAPs) that demonstrate how to create, read, and append records in data files. Additionally, it explains the EOF function and provides sample code for managing employee and student records.

Uploaded by

rauniyarchanda01
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

File Handling in QBASIC # WRITE # or PRINT # : This statement is used to write data to a

#) File: The collection of different data or information or sequential file.


instruction, which is stored in secondary storage device under a Example: WRITE #1, Nm$, RN, M, C
unique file name, is known as file. #) CLOSE #: This statement is used to close the specified data file.
In QBASIC, two different types of files are used they are: Syntax: Close # [File number]
a) Program File Modes of Opening Sequential Data File
b) Data File There are three modes of file that can be opened in QBASIC.
a) Program File: The set of instruction written in a computer a) Output Mode / Writing Mode
language for data processing under unique file name is known b) Input Mode / Reading Mode
as Program file. c) Append Mode/ Update Mode
b) Data File: It is the file that stores data related to a person or Mode Purpose
organization. It contains data such as name, address, class etc. This mode allows creating a new sequential
required for data processing. Output Mode: data file and writing data in it. It starts data
# Types of Data File: always from the beginning of the file. (BOF).
QBASIC supports two types of file handling This mode allows reading/displaying data or
Input Mode: information from the existing sequential data
a) Sequential Access data file :
file.
b) Random Access data File:
This mode allows adding more record in the
Sequential Access to a data files means that the computer system Append Mode: existing sequential file. It starts adding of
reads or writes information to the file sequentially i.e. in serial data always from the end of the file (EOF).
order. In this type of file the data are written and read in one same
order. On the other hand, Random Access to a file means that the 1) WAP to create a sequential data file “[Link]” to stores
computer system can read or write information anywhere in the student’s name, address & Marks of three subjects.
data file. OPEN “[Link]” FOR OUTPUT AS #1
#) Open Statement: It allows a user to open a sequential data file CLS
so that we can write, read or append information in that file. INPUT “Enter the name of student”;N$
Syntax: Open “File Name” For [Mode] AS # File number INPUT “Enter the Address”;A$
Example: Open “[Link]” FOR OUTPUT AS # 1 INPUT “Enter the Marks in Mathematics”;M
It creates and open a new file name “[Link]” to store INPUT “Enter the Marks in Computer”;C
records. INPUT “Enter the Marks in Science”;S
- File name is the name of the file that is to be created. WRITE #1, N$,A$,M, C, S
- Mode specifies the type of operation that is going to be CLOSE #1
performed on data file. END
- File number is positive integer that can be from 1 to 255.
2) WAP to create a sequential file “[Link]” to store 4) WAP to create a sequential data file “[Link]” to store name
employees Name, Contact Number, Address and Post of 10 and marks obtained in English, Maths and Science subjects for
Employees. few students.
OPEN “[Link]” FOR OUTPUT AS #1 OPEN “[Link]” FOR OUTPUT AS #1
CLS TOP:
FOR I = 1 to 10 CLS
INPUT “Enter the name of Employee”; N$ INPUT “Enter the name of student”;N$
INPUT “Enter the Contact Number”;Nu INPUT “Enter the Marks in English”;E
INPUT “Enter the Address”;A$ INPUT “Enter the Marks in Maths”;M
INPUT “Enter the Post of Employee’s”;P$ INPUT “Enter the Marks in Science”;S
WRITE #1, N$, Nu, A$, P$ WRITE #1, N$,E,M,S
NEXT I Input “Do you want to add more Records”;ANS$
CLOSE #1 IF ANS$=”Y” or ANS$=”y” then goto TOP:
END CLOSE #1
END
3) WAP to store books name, writers name and price of the book in a
sequential data file “[Link]”. The program provides the facility
to enter more records as long as the user wants.
OPEN “[Link]” FOR OUTPUT AS #1
TOP:
CLS
INPUT “Enter Book’s Name”;B$
INPUT “Enter Writer’s Name”;W$
INPUT “Enter the price of book”;P
WRITE #1, B$, W$,P
INPUT “Do you want to add more records (Y/N)”;ANS$
IF ANS$=”Y” or ANS$=”y” then GOTO TOP:
CLOSE #1
END
Reading Sequential File 7) WAP to open a sequential data file “[Link]” which
#) INPUT # Statement: This statement is used to read data from contains the employee records, name, address and phone
file and assign them to variable. number and display the records as well as total number of
Syntax: Input # <file number>, variable 1, variable 2, variable 3… records stored in it.
Example: Input #1, N$,C,Add$
OPEN “[Link]” for INPUT AS #1
5) A data file “[Link]” contains Name, Address & Occupation of CLS
a person. WAP to display the record. WHILE NOT EOF(1)
Open “[Link]” FOR Input AS #1 INPUT #1, N$,A$,P
CLS Print N$,A$,P
INPUT #1, N$, A$,O$ F=F+1
PRINT N$,A$,O$ Wend
CLOSE #1 Print “Total number of Records=”;F
END Close #1
#) EOF() : EOF stands for end of file. It is a function used to check END
whether the record pointer reaches at end of file or not.

6) A sequential data file “[Link]” contains Roll Number, 8) A data file “[Link]” consists of Book’s name, Author’s name
Name, English, Nepali and Maths fields. WAP to display all the and price of books. Write a program to count and display the
contents of the data file. total number of records present in the file.

OPEN “[Link]” FOR INPUT AS #1 OPEN “[Link]” for INPUT AS #1


CLS CLS
WHILE NOT EOF(1) WHILE NOT EOF(1)
INPUT #1, RN, N$,E,N,M INPUT #1, BN$,AN$,P
PRINT RN, N$,E,N,M F=F+1
WEND Wend
CLOSE #1 Print “Total number of Records=”;F
END Close #1
END
9) A sequential data file “[Link]” contains name, post and 11) A data file named “[Link]” contains name,
salary files of information about employees. WAP to display all designation and salary of employees. WAP to display all the
the information of employees along with tax amount also. (Tax employees record whose salary is between 10000 to 15000.
is 15% of Salary)
OPEN “[Link]” for INPUT AS #1 OPEN “[Link]” FOR INPUT AS #1
CLS CLS
WHILE NOT EOF(1) WHILE NOT EOF(1)
INPUT #1, N$,P$,S INPUT N$,D$,S
T=15/100*S IF S>=10000 ANS S<=15000 THEN
Print N$,P$,S,T PRINT “ Name =”;N$
Wend PRINT “Designation=”;D$
Close #1 Print “Salary=”;S
END END IF
WEND
10) A Sequential file “[Link]” contains name, address Close #1
and phone number. WAP to display all the information whose END
address is Kadamchowk or Bhanuchowk. 12) A Sequential data file “[Link]” contains name and
marks secured by students in 3 subjects. Assuming that pass
OPEN “[Link]” FOR INPUT AS #1 marks of each subjects is 40, WAP to count the number of pass
CLS students.
WHILE NOT EOF(1) OPEN “[Link]” FOR INPUT AS #1
INPUT #1, N$, A$, P CLS
IF A$= “KADAMCHOWK” OR A$= “BHANUCHOWK” then WHILE NOT EOF(1)
PRINT “Name=”;N$ INPUT #1, N$, E, M, C
PRINT “Address=”;A$ IF E>=40 AND M>=40 AND C>=40 THEN C = C+1
PRINT “Phone Number=”;P WEND
END IF PRINT “Total Pass Students=”;C
WEND CLOSE #1
CLOSE #1 END
END
Appending a data file
Appending a data file means to add extra or additional data
to the end of the data file.

13) A Sequential data file called “[Link]” contains some


records under the field’s name, English, Nepali and Computer.
Write a program to add some more records in the same
sequential data file.
OPEN “[Link]” for APPEND AS #1
CLS
TOP:
Input “Enter Name”;N$
Input “Enter Marks in English”;E
INPUT “Enter Mark sin Nepali”;Ne
INPUT “Enter Mark sin Computer”;C
Write #1, N$, E, Ne, C
INPUT “Do you want to add more”;ANS$
IF ANS$=”Y” or ANS$=”y” then goto TOP:
CLOSE#1
END

Common questions

Powered by AI

The OPEN statement in QBASIC is used to open a sequential data file for writing, reading, or appending. It specifies the file name, operation mode (Output, Input, or Append), and a file number. The mode determines whether the program creates a new file (Output), reads data from an existing file (Input), or adds data to the end of an existing file (Append).

File modes in QBASIC have significant implications for data integrity and efficiency. The Output mode creates a new file, which can overwrite existing data if used improperly, thus risking data loss if existing data is not backed up. Input mode is safe for data integrity as it only reads data, preserving the original content. Append mode is efficient for adding data without altering existing records, maintaining data integrity while allowing file growth. Each mode must be used appropriately to ensure data processing needs are met efficiently without compromising data integrity .

Appending to a data file is preferable in scenarios where new data needs to be added without altering existing records, such as maintaining a transaction log or daily reports, where entries are constantly added. Appending ensures that data integrity is preserved for historical records, and the program's simplicity improves as it avoids merging files or handling multiple file operations. This is particularly useful in maintaining continuous datasets without reprocessing all existing data entries .

Writing data to a sequential file in QBASIC involves using the WRITE # or PRINT # statement after opening the file with the appropriate mode. The statement writes data from variables to the file in the format specified. For example, WRITE #1, N$, A$, M, C, S writes the values of these variables to the file opened with file number 1 .

The INPUT # statement in QBASIC is used to read and assign data from an open file to variables sequentially. It is significant because it allows files to be read line-by-line, assigning each piece of data to specific variables, thereby facilitating the processing of file data. For instance, INPUT #1, N$, C, Add$ reads data from a file with file number 1 into the variables N$, C, and Add$ .

Sequential access files are straightforward and efficient for reading or writing data in a predetermined order, making them suitable for tasks like logs or streaming data. However, they are less efficient for random data retrieval or updates, especially in large datasets, as this requires reading through the entire file. Random access files, though more complex to implement, offer faster retrieval and flexibility when accessing specific data points, beneficial for databases where updates and retrievals exceed mere reading or writing. The ideal choice depends on application needs, balancing simplicity and performance .

In QBASIC, modularity and flexibility can be achieved by structuring file operation programs to use loops and conditional statements for dynamic input handling. For instance, using a loop construct alongside the INPUT statement allows repeated data entry or file operations until a condition is met, such as EOF() or user input choices. Functions and subroutines can further encapsulate file operations to allow code reuse and adaptability across different file contexts or data categories. Implementing user prompts and input validation ensures the program handles various user inputs logically and efficiently .

In QBASIC, the two main types of files are Program Files and Data Files. Program Files contain a set of instructions written in a computer language for data processing, while Data Files store data related to a person or organization, such as names, addresses, and classes. Program Files are designed for executing processes, while Data Files are designed for storing and retrieving data .

The EOF() function checks whether the record pointer has reached the end of a file during data reading operations. It is crucial for determining when to stop reading data in a loop, preventing errors or infinite loops by ensuring the file doesn't attempt to read beyond its contents. For example, using 'WHILE NOT EOF(1)' ensures that the program continues to read input until the file's end is reached .

Sequential access means that the computer reads or writes information in serial order, making data retrieval straightforward but potentially time-consuming for large files, as the system must start from the beginning to access specific data. In contrast, random access allows the computer to read or write information at any point in the file, facilitating faster data retrieval for specific entries, though it may require more complex programming to implement .

You might also like