We know the meaning of file in the computer system. A file is a collection of data or information. Almost all information stored in a computer must be in a file. In our daily life, we are creating different types of files such as paint files, WordPad files, excel files, text file,s etc. We also save our QBASIC programs with .BAS file extension, is also a type of file. So, we can say that everything on a computer is stored in a file.
There are two types of files in QBASIC, they are Sequential files and Random files. Here, we will study only sequential files.
So, at first what is file handling and why it is required, and why it is used?
File handling is simply writing programs and storing information in files for future use which helps in managing data and working with data, we can search data from files, delete them, and do much more.
Let's begin learning about it. Before we start writing programs, we need to know how to deal with files. Let us know about that. To create a file we use OUTPUT mode, to read data from files we use INPUT mode, and to add records in the file we use APPEND mode.
Creating a Data File
To create a file we have to open the data file in OUTPUT mode. The syntax is given below
Syntax:
OPEN "Filename" FOR OUTPUT AS #1
OR
OPEN OUTPUT #1, "Filename"
Here we have two syntaxes for creating a data file in QBASIC. We can use any one syntax to create a data file.
Example
OPEN "student.txt" FOR OUTPUT AS #1
Int the above line of code we use OPEN keyword to open the file so that we can access them. "student.txt" is a name of data file which we want to create and we use keyword FOR OUTPUT AS to create a data file. #1 is the buffer number of the file (in this case it is 1).
style="text-align: justify:"> Let's see the complete example of creating and writing some data into data file.
Program to create a data "student.txt" sequential data file to store records of students.
CLS
OPEN "student.txt" FOR OUTPUT AS #1
INPUT "Enter Name : "; name$
INPUT "Enter Class : "; c
INPUT "Enter Section : "; s$
INPUT "Enter Subject : "; sub$
INPUT "Enter Address : "; add$
WRITE #1, name$, c, s$, sub$, add$
CLOSE #1
END
In the above program we have opened data file "student.txt" in OUTPUT mode. It will create "student.txt" data file. After that we take some records of student from the user such as name, class, section, subject and address by using INPUT statement. In line 8, we write those records into "student.txt" data file by using WRITE keyword. Finally we have closed the data file by using CLOSE statement. When we run this program, it will ask us to enter name, class, section, subject and address of the student. It will create "student.txt" file inside the qbasic folder.
Adding new records in the existing data file
To add new records in the existing data file we have to open data file in APPEND mode.Syntax is given below.
Syntax:
OPEN "Filename" FOR APPEND AS #1
OR
OPEN APPEND #1, "Filename"
Example
CLS
OPEN "student.txt" FOR APPEND AS #1
INPUT "Enter Name : "; name$
INPUT "Enter Class : "; c
INPUT "Enter Section : "; s$
INPUT "Enter Subject : "; sub$
INPUT "Enter Address : "; add$
WRITE #1, name$, c, s$, sub$, add$
CLOSE #1
END
Program is same as first example. Only difference is that. To add new records in the existing data file we need to use APPEND instead of OUTPUT. It will add new record in "student.txt" data file.
If you want to add more records in the same data file. Then we can use loop or goto statement. For that look at the following example.
CLS
OPEN "student.txt" FOR APPEND AS #1
DO
INPUT "Enter Name : "; name$
INPUT "Enter Class : "; c
INPUT "Enter Section : "; s$
INPUT "Enter Subject : "; sub$
INPUT "Enter Address : "; add$
WRITE #1, name$, c, s$, sub$, add$
INPUT "Do you want to add more records? (Y/N)"; ch$
LOOP WHILE ch$ = "Y" OR ch$ = "y"
CLOSE #1
END
After entering the first record, program will prompt the user to enter next record. If we press capital letter "Y" or small letter "y" then program will repeat the same statement and allow the user to enter next record otherwise loop will be terminated.
Reading Data File
To read data from data file, we have to open file in INPUT mode. INPUT mode is used to read data from the data file. Syntax is given below.
Syntax:
OPEN "Filename" FOR INPUT AS #1
OR
OPEN INPUT #1, "Filename"
Example
CLS
OPEN "student.txt" FOR INPUT AS #1
DO WHILE NOT EOF(1)
INPUT #1, name$, c, sec$, sub$, add$
PRINT name$, c, sec$, sub$, add$
LOOP
CLOSE #1
END
This program will read and display the record of "student.txt", which we have created in the first example. In the above program, we've used do while not eof(1) which means loop runs until it reaches end of the file, means until the data of end line of the file is processed, the loop continues to run. In the 4th line we saw INPUT #1, name$, c, sec$, sub$, add$, which meas data are being read from the file and then we've printed the variables in the next line. Again loop runs when it reaches loop statement. This process lasts until the loop processes last record of the file. Finally file is closed and we've declared the program ends.
In this post we learned the basic operations on data file. Main key points on file is this much. In this way we can create our own data file to store different types of records.