File handling in QBASIC Class 10 Computer Science
Introduction
All the programs need data to manipulate information. Whenever you execute a program and the program asks data every time, it makes the users job tedious. So in order to remove the inconvenience you need to store data for future uses. In QBASIC you can save the data into a separate file called a data file. So there are two types of files in QBASIC. They are:
Program File
Data File
Program File
A program file is a set of instructions that need for data processing. Whatever task you perform that is done by executing the instructions of a program files. It has .BAS as an extension.
Data File
A data file is the collection of related data stored in a secondary storage. Such a collection of data in a row is known as record. A record in a data file contains the detailed information of a person or anything. For example, a record of a data file related to ‘Result of students’ consists of a student names, marks in different subjects, percent, division, and result of students. Each individual data of a record is known as field. You need to create a file to store data input by the user on the disk for later use. To use data of a data file you need to write such a program that could handle the data file.
Types of Data File
Depending on the way in which the data is stored and accessed, data files are categoried into two groups. They are:
Sequential Access File and
Random Access File
Sequential Access Files
In a sequential access data file, data is stored in sequential order. The data of a sequential access file can only be accessed sequentially. For example, if a sequential file has stored name of a person, address and age of some people then data of this file need to access in the same order as the data is stored in a row. Since the accessing of data from the sequential data file is done in sequential order, accessing data takes long time if the data file contains a large volume of data. In the sequential data file, you cannot change the existing entry or insert a new entry. To do this you should copy the entire content to another file making the change on the way and then copy the correct data back into the original fine.
Random Access File
Random access file allows you to write or read data from any location of the file. That means you can read or write any record directly in a random file without searching through all the records that precede it. Thus, reading and writing of data is faster.
Opening a Data File
A sequential data file is opened before you can use it. The opening of a data file may be for different purpose like:
To open of sequential data file, OPEN statement is used.
Syntax 1:
OPEN<filename> FOR <MODE> AS # Filenumber
Where,
Filename is the name of the data file
MODE is the mode of operation of data file like: OUTPUT, INPUT, and APPEND
The different modes and their purpose are given in the table below:
File number is a number from 1 to 255 that identifies the data file.
Examples:
OPEN “student.dat” FOR OUTPUT AS # 1
In this example a data file named ‘student.dat’ is opened in output mode. That means a new data file named ‘student.dat’ is created and assigned 1 as a file number.
OPEN “Marks.dat” FOR INPUT AS # 2
In this example it opens an existing data file named ‘Marks.dat’ in INPUT mode so you can retrieve records of the data file and manipulate information.
OPEN “Info.dat” FOR APPEND AS # 3
In this example, it opens an existing data file ‘Info.dat’ in APPEND mode so you can add more records to the file.
Syntax 2:
OPEN mode, #filenumber, filename
Where,
Mode is the operation mode of a file. The mode value may be O, I, or A
For Example:
OPEN “O”, #1, “student.dat”
OPEN “I”, #2, “student.dat”
OPEN “A”. #3, student.dat”
The first statement creates a data file named “student.dat”
The second statement opens and allows you to retrieve data from “student.dat”
The third statement opens the existing file “student.dat” and allows you to add new record.
As you have finished using the data file or files, you need to close the data files. The CLOSE statement is used to close the data files.
Syntax:
CLOSE #Filenumber, #Filenumber ….
Where,
Filenumber is the number used while opening the file.
For example:
CLOSE #1, #3
It closes both the data file that has file number 1 and 3.
CLOSE
It closes all the opened data files.
Writing Data to a New Data File
To write data in a new sequential data file you need to do the following tasks.
PRINT #file_number, list of variables
Or
WRITE #file_number, list of variables
Where
File_number refers to the file where data is stored
list of variables specifies the data that is to be written to the file. Variables are separated commas
The following are the differences between PRINT# and WRITE# statements.
Note:
OPEN “PERSON.DAT” FOR OUTPUT AS #1
CLS
INPUT” Enter name”; N$
INPUT” Enter age”; A$
INPUT” Enter date of birth”; D$
INPUT” Enter Sex”; S$
WRITE #1, N$, A$, D$, S$
CLOSE #1
END
This program creates a data file ‘teldir.dat’ to store Name, Address and Telephone number of employees according to the need of the user.
OPEN “teldir.dat” FOR OUTPUT AS #1
DO
CLS
INPUT “ENTER NAME”; N$
INPUT “ENTER ADDRESS”; A$
INPUT “ENTER TELEPHONE NUMBER”; T
WRITE #1, N$, A$, T$
INPUT “DO YOU WANT TO CONTINUE(Y/N)”; CH$
LOOP WHILE UCASE$(CH$) = ”Y”
CLOSE #1
END
This program creates a data file to store the records of few employees having Name, Address, Post, Gender and Salary fields.
OPEN “std.dat” FOR OUTPUT AS #1
CLS
DO
INPUT “Enter Name”; N$
INPUT “Enter Address”; A$
INPUT “Enter Post”; P$
INPUT “Enter gender”; G$
INPUT “Enter Salary”; S
WRITE #1, N$, A$, P$, G$, S
INPUT “Do you want to continue”; CH$
LOOP WHILE UCASE$(CH$) = ”Y”
CLOSE #1
END
This program creates a data file to store Roll no., Name, Class, and Address of any five students.
OPEN “STD.DAT” FOR OUTPUT AS #1
CLS
FOR I = 1 TO 5
INPUT” ENTER ROLL NUMBER”; RN
INPUT” ENTER NAME”; N$
INPUT” ENTER CLASS”; C
INPUT” ENTER ADDRESS”; A$
WRITE #1, RN, N$, C, A$
NEXT I
CLOSE #1
END
Reading Data from a File
The contents of the existing file can be read and information can be produced. To read the data contents of the existing file, the file must be opened in INPUT mode. When the file is opened in INPUT mode, you can retrieve data of the file in sequential order. The retrieval of records from the data file needs to be continued till all the records has not been retrieved.
A program written from reading data from a sequential file must perform the following task:
INPUT# Statement
The INPUT# statement reads data of a sequential data file and store them in variables. It reads data of the fine in the same order as they are stored.
Syntax:
INPUT# filenumber, list of variables
For example:
INPUT #1, Name$, Address$, Tel$
This statement retrieves data of the file number 1 and in the sequential order and stores them in Name$, Address$ and Tel$ variables.
LINE INPUT# Statement
The LINE INPUT# statement reads an entire line ignoring delimities, from a sequential file and assign to a string variable.
Syntax:
LINE INPUT # filenumber, variable$
Where,
Filenumber is the number under which the file was opened.
Variable$ holds a line of characters read from a file. It can hold up to 255 characters.
LINE INPUT# reads all characters in the sequential file up to a carriage return which was inserted by the PRINT# statement.
Note: The data to be read from a data file must be matched with the list of variables.
EOF () FUNCTION
A data file may contain more than one record. To read all the records of the file you need to execute reading process more than once. That means you need to execute INPUT# statement again and again to retrieve all the records. Since you don't know the number of records in the data file you need to check whether the record pointer reaches at the end of file or not. You need to retrieve records till the end of file. To check whether the record pointer reaches at the end of file or not you need to use EOF () function.
EOF stands for End of File. This function is used for testing the end of file. If the record pointer reaches at the end of file, then it returns (-1). If there are no records to be read from a data file, EOF () function returns False (0).
Syntax:
EOF(FILENUMBER)
Where,
FILENUMBER is the number of the open file.
Note:
If you know many records to be retrieve from a file you can use loop structure statement like FOR…NEXT.
Examples:
This program displays all the contents of that data file where a sequential data file called “Marks.dat” contains Name, English, Nepali, Math’s and Science Fields.
OPEN “Marks.dat” FOR INPUT AS #1
CLS
PRINT “NAME”, “ENGLISH”, “NEPALI”, “MATHS”, “SCIENCE”
DO WHILE NOT EOF (1)
INPUT #1, N$, E, N, M, S
PRINT N$, E, N, M, S
LOOP
CLOSE #1
END
REM a program to display all the information of employees whose salary is greater than 15000 and less than 40000.
OPEN “Salary.dat” FOR INPUT AS #1
CLS
DO WHILE NOT EOF (1)
INPUT #1, N$, P$, S
IF S>15000 AND S<40000 THEN
PRINT N$, P$, S
LOOP
CLOSE #1
END
All the programs need data to manipulate information. Whenever you execute a program and the program asks data every time, it makes the users job tedious. So in order to remove the inconvenience you need to store data for future uses. In QBASIC you can save the data into a separate file called a data file. So there are two types of files in QBASIC. They are:
Program File
Data File
Program File
A program file is a set of instructions that need for data processing. Whatever task you perform that is done by executing the instructions of a program files. It has .BAS as an extension.
Data File
A data file is the collection of related data stored in a secondary storage. Such a collection of data in a row is known as record. A record in a data file contains the detailed information of a person or anything. For example, a record of a data file related to ‘Result of students’ consists of a student names, marks in different subjects, percent, division, and result of students. Each individual data of a record is known as field. You need to create a file to store data input by the user on the disk for later use. To use data of a data file you need to write such a program that could handle the data file.
Types of Data File
Depending on the way in which the data is stored and accessed, data files are categoried into two groups. They are:
Sequential Access File and
Random Access File
Sequential Access Files
In a sequential access data file, data is stored in sequential order. The data of a sequential access file can only be accessed sequentially. For example, if a sequential file has stored name of a person, address and age of some people then data of this file need to access in the same order as the data is stored in a row. Since the accessing of data from the sequential data file is done in sequential order, accessing data takes long time if the data file contains a large volume of data. In the sequential data file, you cannot change the existing entry or insert a new entry. To do this you should copy the entire content to another file making the change on the way and then copy the correct data back into the original fine.
Random Access File
Random access file allows you to write or read data from any location of the file. That means you can read or write any record directly in a random file without searching through all the records that precede it. Thus, reading and writing of data is faster.
Opening a Data File
A sequential data file is opened before you can use it. The opening of a data file may be for different purpose like:
- To create a new file and write data
- To read data of the existing data file
- To add some more data in the existing data file
To open of sequential data file, OPEN statement is used.
Syntax 1:
OPEN<filename> FOR <MODE> AS # Filenumber
Where,
Filename is the name of the data file
MODE is the mode of operation of data file like: OUTPUT, INPUT, and APPEND
The different modes and their purpose are given in the table below:
Mode |
Purpose |
OUTPUT Mode |
To create a new data file and write data in
it. |
INPUT Mode |
To retrieve the contents of the existing data
file. |
APPEND Mode |
To add more records in the existing file. |
File number is a number from 1 to 255 that identifies the data file.
Examples:
OPEN “student.dat” FOR OUTPUT AS # 1
In this example a data file named ‘student.dat’ is opened in output mode. That means a new data file named ‘student.dat’ is created and assigned 1 as a file number.
OPEN “Marks.dat” FOR INPUT AS # 2
In this example it opens an existing data file named ‘Marks.dat’ in INPUT mode so you can retrieve records of the data file and manipulate information.
OPEN “Info.dat” FOR APPEND AS # 3
In this example, it opens an existing data file ‘Info.dat’ in APPEND mode so you can add more records to the file.
Syntax 2:
OPEN mode, #filenumber, filename
Where,
Mode is the operation mode of a file. The mode value may be O, I, or A
For Example:
OPEN “O”, #1, “student.dat”
OPEN “I”, #2, “student.dat”
OPEN “A”. #3, student.dat”
The first statement creates a data file named “student.dat”
The second statement opens and allows you to retrieve data from “student.dat”
The third statement opens the existing file “student.dat” and allows you to add new record.
As you have finished using the data file or files, you need to close the data files. The CLOSE statement is used to close the data files.
CLOSE #Filenumber, #Filenumber ….
Where,
Filenumber is the number used while opening the file.
For example:
CLOSE #1, #3
It closes both the data file that has file number 1 and 3.
CLOSE
It closes all the opened data files.
Writing Data to a New Data File
To write data in a new sequential data file you need to do the following tasks.
- Open data file in OUTPUT mode using OPEN statement.
- Get the data from the user.
- Write data in a sequential data file using either PRINT# or WRITE# statement.
- Repeat the above steps.
- Close the date of file.
PRINT #file_number, list of variables
Or
WRITE #file_number, list of variables
Where
File_number refers to the file where data is stored
list of variables specifies the data that is to be written to the file. Variables are separated commas
The following are the differences between PRINT# and WRITE# statements.
WRITE# |
WRITE# |
WRITE# inserts commas between the data items. |
PRINT# statement adds spaces between data
items while storing data. |
It encloses strings in double quotation marks. |
It does not encloses strings in any quotation
marks. |
WRITE# statement stores values to a file as a
separated data item so, data in a row can be read individually. |
PRINT# statements writes values to a file as a
single data, so data in a row can’t be read individually. |
Note:
- If an existing file is opened in OUTPUT mode, then it overwrites the contents of the existing file. so all the previous data of the file will be lost.
- The contents of a data file can be viewed in any text editor by using TYPE command in DOS prompt.
OPEN “PERSON.DAT” FOR OUTPUT AS #1
CLS
INPUT” Enter name”; N$
INPUT” Enter age”; A$
INPUT” Enter date of birth”; D$
INPUT” Enter Sex”; S$
WRITE #1, N$, A$, D$, S$
CLOSE #1
END
This program creates a data file ‘teldir.dat’ to store Name, Address and Telephone number of employees according to the need of the user.
OPEN “teldir.dat” FOR OUTPUT AS #1
DO
CLS
INPUT “ENTER NAME”; N$
INPUT “ENTER ADDRESS”; A$
INPUT “ENTER TELEPHONE NUMBER”; T
WRITE #1, N$, A$, T$
INPUT “DO YOU WANT TO CONTINUE(Y/N)”; CH$
LOOP WHILE UCASE$(CH$) = ”Y”
CLOSE #1
END
This program creates a data file to store the records of few employees having Name, Address, Post, Gender and Salary fields.
OPEN “std.dat” FOR OUTPUT AS #1
CLS
DO
INPUT “Enter Name”; N$
INPUT “Enter Address”; A$
INPUT “Enter Post”; P$
INPUT “Enter gender”; G$
INPUT “Enter Salary”; S
WRITE #1, N$, A$, P$, G$, S
INPUT “Do you want to continue”; CH$
LOOP WHILE UCASE$(CH$) = ”Y”
CLOSE #1
END
This program creates a data file to store Roll no., Name, Class, and Address of any five students.
OPEN “STD.DAT” FOR OUTPUT AS #1
CLS
FOR I = 1 TO 5
INPUT” ENTER ROLL NUMBER”; RN
INPUT” ENTER NAME”; N$
INPUT” ENTER CLASS”; C
INPUT” ENTER ADDRESS”; A$
WRITE #1, RN, N$, C, A$
NEXT I
CLOSE #1
END
Reading Data from a File
The contents of the existing file can be read and information can be produced. To read the data contents of the existing file, the file must be opened in INPUT mode. When the file is opened in INPUT mode, you can retrieve data of the file in sequential order. The retrieval of records from the data file needs to be continued till all the records has not been retrieved.
A program written from reading data from a sequential file must perform the following task:
- Open the data file in INPUT mode.
- Read or retrieve data in rows one by one till the end of file.
- Manipulate retrieve data.
- Close the file.
INPUT# Statement
The INPUT# statement reads data of a sequential data file and store them in variables. It reads data of the fine in the same order as they are stored.
Syntax:
INPUT# filenumber, list of variables
For example:
INPUT #1, Name$, Address$, Tel$
This statement retrieves data of the file number 1 and in the sequential order and stores them in Name$, Address$ and Tel$ variables.
LINE INPUT# Statement
The LINE INPUT# statement reads an entire line ignoring delimities, from a sequential file and assign to a string variable.
LINE INPUT # filenumber, variable$
Where,
Filenumber is the number under which the file was opened.
Variable$ holds a line of characters read from a file. It can hold up to 255 characters.
LINE INPUT# reads all characters in the sequential file up to a carriage return which was inserted by the PRINT# statement.
Note: The data to be read from a data file must be matched with the list of variables.
EOF () FUNCTION
A data file may contain more than one record. To read all the records of the file you need to execute reading process more than once. That means you need to execute INPUT# statement again and again to retrieve all the records. Since you don't know the number of records in the data file you need to check whether the record pointer reaches at the end of file or not. You need to retrieve records till the end of file. To check whether the record pointer reaches at the end of file or not you need to use EOF () function.
EOF stands for End of File. This function is used for testing the end of file. If the record pointer reaches at the end of file, then it returns (-1). If there are no records to be read from a data file, EOF () function returns False (0).
Syntax:
EOF(FILENUMBER)
Where,
FILENUMBER is the number of the open file.
Note:
If you know many records to be retrieve from a file you can use loop structure statement like FOR…NEXT.
Examples:
This program displays all the contents of that data file where a sequential data file called “Marks.dat” contains Name, English, Nepali, Math’s and Science Fields.
OPEN “Marks.dat” FOR INPUT AS #1
CLS
PRINT “NAME”, “ENGLISH”, “NEPALI”, “MATHS”, “SCIENCE”
DO WHILE NOT EOF (1)
INPUT #1, N$, E, N, M, S
PRINT N$, E, N, M, S
LOOP
CLOSE #1
END
REM a program to display all the information of employees whose salary is greater than 15000 and less than 40000.
OPEN “Salary.dat” FOR INPUT AS #1
CLS
DO WHILE NOT EOF (1)
INPUT #1, N$, P$, S
IF S>15000 AND S<40000 THEN
PRINT N$, P$, S
LOOP
CLOSE #1
END
REM program to display NAME,
AGE, CITY, and TELEPHONE fields of any 5 persons.
OPEN “Marks.dat” FOR INPUT
AS #1
CLS
FOR I = 1 TO 5
INPUT #1, N$, A, C$, T$
PRINT N$, A, C$, T$
NEXT I
CLOSE #1
END
Adding Data to a Sequential File
You can add new records at the end existing file. To add any new data in the file you need to open the file in APPEND mode. To add records in the existing file, follow these steps:
For Example:
OPEN “Rec.dat” FOR APPEND AS #1
This statement will open the file named ‘Rec.dat’ in an append mode and position the record pointer at the end of the file.
Note:
Example:
OPEN “RESULT.DAT” FOR APPEND AS #1
CLS
DO
INPUT “Enter name”; N$
INPUT “Enter address”; A$
INPUT “Enter marks in three different subjects”; M1, M2, M3
WRITE #1, N$, A$, M1, M2, M3
INPUT” Do you want to continue(Y/N)”;CH$
LOOP WHILE UCASE$(CH$)=”Y”
CLOSE #1
END
File Management Statements and Functions
INPUT# Function
This function returns a part of a string from a file.
Syntax:
INPUT$ (N, #filenumber)
Where,
N is the number of characters or byte to be extracted
Filenumber is the number of an open file.
Example:
CLS
OPEN “TRY.DAT” FOR OUTPUT AS #1
PRINT #1, “This is a new book on the basis of revised course”
CLOSE #1
OPEN “TRY.DAT” FOR INPUT AS #1
CHECK$=INPUT$(18, #1)
CLOSE #1
PRINT CHECK$
END
This program displays “This is a new book”
FILES Statement
This statement displays a list of files of a disk or a directory. This is similar to DIR command of MS-DOS.
Syntax:
FILES (File specification)
Examples:
FILES Displays list of files of default directory.
FILES “*.DAT” Displays list of files having .DAT extension.
KILL Statement
This statement is used to delete files. This is same as MS-DOS DEL command.
Syntax:
KILL file$
Where,
File$ identifies the file or files to delete. It may include a path and the DOS wildcard (* and ?)
Examples:
KILL “student.dat” Deletes “student.dat” file of the default directory.
KILL “* .dta” Deletes all files having .dta extension of the default directory.
NAME Statement
This statement renames a file. It is like REN command of MS-DOS
Synatx:
NAME old filename$ AS new filename$
Where,
Old filename and new filename refers the existing file and the new name for the file respectively. Each name may include a path.
Example:
NAME “Student.bas” AS “Record.bas”
It renames Student.bas as Record.bas
CHDIR Statement
CHDIR statement changes the default directory.
Syntax:
CHDIR pathname$
Where,
Pathname$ is the path of the new default directory. The pathname$ has following format:
Drive\directory\directory\.....
Note:
CHDIR statement changes the default directory but not the default drive.
Examples:
CHDIR”\”
It changes the default directory to the root directory of the default drive.
CHDIR “C:\BASWORK
It changes the default directory to BASWORK of drive C.
MKDIR Statement
MKDIR creates a subdirectory. This statement is similar to MS-DOS MD command.
Syntax:
MKDIR pathname$
Where,
Pathname$ specifies a subdirectory to create.
Example:
MKDIR “C:\QBASIC\WORK”
It creates WORK subdirectory inside QBASIC directory of C drive.
MKDIR “work”
It creates a work directory in the default drive.
RMDIR Statement
RMDIR removes a subdirectory.
Syntax:
RMDIR pathname$
Where,
Pathname$ specifies a subdirectory to remove.
Example:
RMDIR ”QBASIC\WORK”
It removes WORK subdirectory of QBASIC directory.
SHELL Command
The SHELL command is used to exit temporarily from the QBASIC environment to DOS prompt. To return back to the QBASIC environment you need to apply EXIT command.
Syntax:
SHELL (commandstring$)
Where, commandstring$ is the name of the DOS command.
Example:
SHELL
It exits temporarily from the QBASIC environment to DOS prompt.
SHELL “DIR”
It displays the content of the default directory.
SYSTEM Command
It closes all the files and exits permanently from the QBASIC.
Updating a Sequential Data File
You may need to change the value of fields or delete some records or insert some new records. The process of changing information of data file is known as updating. To update data record of a sequential file, follow these steps:
Making a duplicate data file
Example:
CLS
OPEN ”TELDIR.DAT” FOR INPUT AS #1
OPEN “DUPLI.DAT” FOR OUTPUT AS #2
DO WHILE NOT EOF(1)
INPUT#1, Name$, Address$, Tel$
WRITE#2, Name$, Address$, Tel$
LOOP
CLOSE #1, #2
END
Modifying the contents of the data file
Example:
CLS
OPEN “Marks.dat” FOR INPUT AS #1
OPEN “Dupli.dat” FOR OUTPUT AS #2
DO WHILE NOT EOF(1)
INPUT #1, Name$, eng, nep, sci, comsci
Comsci=comsci+10
WRITE #2, Name$, eng, nep, sci, comsci
LOOP
CLOSE #1, #2
KILL “Marks.dat”
NAME “Dupli.dat” AS “Marks.dat”
END
Deleting a record of a file
Example:
CLS
OPEN “STD.DAT FOR INPUT AS #1
OPEN “TEMP.DAT” FOR OUTPUT AS #2
INPUT” Enter roll number of student to delete”; rollnum
DO UNTIL EOF(1)
INPUT#1, m, name$, class, math, computer, English
IF rollnum<> m THEN
WRITE #2, m, name$, class, math, computer, English
END IF
LOOP
CLOSE #1, #2
KILL “STD.DAT”
NAME “TEMP.DAT” AS “STD.DAT”
END
CLS
FOR I = 1 TO 5
INPUT #1, N$, A, C$, T$
PRINT N$, A, C$, T$
NEXT I
CLOSE #1
END
Adding Data to a Sequential File
You can add new records at the end existing file. To add any new data in the file you need to open the file in APPEND mode. To add records in the existing file, follow these steps:
- Open the existing data file in APPEND mode.
- Accept the data of from a user and store them in variables.
- Write the contents of the variables in the data file by using WRITE# statement.
- Ask more data to be added or not. If the user wants to add more data then repeat the step 2, 3 and 4.
- Close the data file.
For Example:
OPEN “Rec.dat” FOR APPEND AS #1
This statement will open the file named ‘Rec.dat’ in an append mode and position the record pointer at the end of the file.
Note:
- When you use APPEND mode to open a file, it places the record pointer at the end of the file and allows you to add more records in it.
- If a file does not already exist, then it creates a new file and allows you to ass records in it. Just like a file opened in OUTPUT mode.
Example:
OPEN “RESULT.DAT” FOR APPEND AS #1
CLS
DO
INPUT “Enter name”; N$
INPUT “Enter address”; A$
INPUT “Enter marks in three different subjects”; M1, M2, M3
WRITE #1, N$, A$, M1, M2, M3
INPUT” Do you want to continue(Y/N)”;CH$
LOOP WHILE UCASE$(CH$)=”Y”
CLOSE #1
END
File Management Statements and Functions
INPUT# Function
This function returns a part of a string from a file.
Syntax:
INPUT$ (N, #filenumber)
Where,
N is the number of characters or byte to be extracted
Filenumber is the number of an open file.
Example:
CLS
OPEN “TRY.DAT” FOR OUTPUT AS #1
PRINT #1, “This is a new book on the basis of revised course”
CLOSE #1
OPEN “TRY.DAT” FOR INPUT AS #1
CHECK$=INPUT$(18, #1)
CLOSE #1
PRINT CHECK$
END
This program displays “This is a new book”
FILES Statement
This statement displays a list of files of a disk or a directory. This is similar to DIR command of MS-DOS.
Syntax:
FILES (File specification)
Examples:
FILES Displays list of files of default directory.
FILES “*.DAT” Displays list of files having .DAT extension.
KILL Statement
This statement is used to delete files. This is same as MS-DOS DEL command.
Syntax:
KILL file$
Where,
File$ identifies the file or files to delete. It may include a path and the DOS wildcard (* and ?)
Examples:
KILL “student.dat” Deletes “student.dat” file of the default directory.
KILL “* .dta” Deletes all files having .dta extension of the default directory.
NAME Statement
This statement renames a file. It is like REN command of MS-DOS
Synatx:
NAME old filename$ AS new filename$
Old filename and new filename refers the existing file and the new name for the file respectively. Each name may include a path.
Example:
NAME “Student.bas” AS “Record.bas”
It renames Student.bas as Record.bas
CHDIR Statement
CHDIR statement changes the default directory.
Syntax:
CHDIR pathname$
Where,
Pathname$ is the path of the new default directory. The pathname$ has following format:
Drive\directory\directory\.....
Note:
CHDIR statement changes the default directory but not the default drive.
Examples:
CHDIR”\”
It changes the default directory to the root directory of the default drive.
CHDIR “C:\BASWORK
It changes the default directory to BASWORK of drive C.
MKDIR Statement
MKDIR creates a subdirectory. This statement is similar to MS-DOS MD command.
Syntax:
MKDIR pathname$
Where,
Pathname$ specifies a subdirectory to create.
Example:
MKDIR “C:\QBASIC\WORK”
It creates WORK subdirectory inside QBASIC directory of C drive.
MKDIR “work”
It creates a work directory in the default drive.
RMDIR Statement
RMDIR removes a subdirectory.
Syntax:
RMDIR pathname$
Where,
Pathname$ specifies a subdirectory to remove.
Example:
RMDIR ”QBASIC\WORK”
It removes WORK subdirectory of QBASIC directory.
SHELL Command
The SHELL command is used to exit temporarily from the QBASIC environment to DOS prompt. To return back to the QBASIC environment you need to apply EXIT command.
Syntax:
SHELL (commandstring$)
Where, commandstring$ is the name of the DOS command.
Example:
SHELL
It exits temporarily from the QBASIC environment to DOS prompt.
SHELL “DIR”
It displays the content of the default directory.
SYSTEM Command
It closes all the files and exits permanently from the QBASIC.
Updating a Sequential Data File
You may need to change the value of fields or delete some records or insert some new records. The process of changing information of data file is known as updating. To update data record of a sequential file, follow these steps:
- Open the original file in INPUT mode to read the data.
- Open a new temporary file in OUTPUT mode to store necessary data.
- Search the data of the original file and write the required data in the temporary file.
- Repeat steps 3 until all the data is written to the temporary file.
- Close both the files.
- Delete the original file.
- Assign name of original file to the temporary file.
- Close the file.
Making a duplicate data file
Example:
CLS
OPEN ”TELDIR.DAT” FOR INPUT AS #1
OPEN “DUPLI.DAT” FOR OUTPUT AS #2
DO WHILE NOT EOF(1)
INPUT#1, Name$, Address$, Tel$
WRITE#2, Name$, Address$, Tel$
LOOP
CLOSE #1, #2
END
Modifying the contents of the data file
Example:
CLS
OPEN “Marks.dat” FOR INPUT AS #1
OPEN “Dupli.dat” FOR OUTPUT AS #2
DO WHILE NOT EOF(1)
INPUT #1, Name$, eng, nep, sci, comsci
Comsci=comsci+10
WRITE #2, Name$, eng, nep, sci, comsci
LOOP
CLOSE #1, #2
KILL “Marks.dat”
NAME “Dupli.dat” AS “Marks.dat”
END
Deleting a record of a file
Example:
CLS
OPEN “STD.DAT FOR INPUT AS #1
OPEN “TEMP.DAT” FOR OUTPUT AS #2
INPUT” Enter roll number of student to delete”; rollnum
DO UNTIL EOF(1)
INPUT#1, m, name$, class, math, computer, English
IF rollnum<> m THEN
WRITE #2, m, name$, class, math, computer, English
END IF
LOOP
CLOSE #1, #2
KILL “STD.DAT”
NAME “TEMP.DAT” AS “STD.DAT”
END
Comments
Post a Comment