File processing /File Handling in QBasic
File processing /File Handling in QBasic
Types of files in QBasic
a. Program file
b. Data file
Program file: A program file has a set of instruction that is needed for processing data whatever tasks needs to be performed that is done by executing a program file. Program files do not store data for future use.
Data file: A data file is a collection of related data stored in a secondary storage. In order to store the output of the program in the disk for later use, data file needs to be created using a program.
Types of data files
Depending on the way in which the data is stored and accessed. Data files are categorized as:
i. Sequential access file
ii. Random access file
Sequential access file: In a sequential access data file, data are stored in sequential order. The data of a sequential access file can only be accessed sequentially.
Disadvantages of sequential access file.
a. A particular record of a file cannot be accessed directly. So the time taken to access the data is longer.
Random access file :A user can read or write any record directly in a random access file. Thus, reading and writing of data is faster compared to sequential access file.
Opening a data file
Points to remember
I. A sequential data file is needed to open before it can be used.
II. Opening of a data file could be for different purposes such as:
a. To write data
b. To read data
c. To delete or add some more data etc.
As the requirement, a sequential files need to be opened in different mode. The different mode and their purpose are as follows.
MODE | Purpose |
OUTPUT | To create a new data file and write data. |
INPUT | To retrieve or read the contents of an existing data file. |
APPEND | To add more records in the existing data file |
File-Input-Output Statements
The commands under this group enables us to save data to a file. So that the file can be retrieved later, for printing, adding more data at the end of the file or for modifying the saved data.
The commands under Input/Output files are :
a. OPEN
b. OUTPUT
c. INPUT
d. APPEND
e. LINE INPUT
f. WRITE
g. CLOSE
h. EOF
OPEN
FUNCTION – Open a file or device.
Syntax – Open <File name> FOR <MODE>AS # FILENUMBER
OUTPUT
FUNCTION- To create a new data file and write data.
Syntax: OPEN <FILENAME> FOR <OUTPUT>AS # FILENUMBER
INPUT
FUNCTION : Reads the input from the keyboard or a file.
Syntax: Open < FILE NAME > FOR < INPUT>AS # FILENUMBER
APPEND
FUNCTION : Allows to add records to an existing data file.
Syntax : OPEN < FILENAME> FOR < APPEND> AS # FILENUMBER
LINE INPUT
FUNCTION : It reads a line of up to 255 characters from a keyboard or a file.
Syntax : LINE INPUT # Filename %, Variable $
WRITE
FUNCTION- It writes data to a string or a sequential file.
Syntax- WRITE # FILENUMBER%, expression list.
CLOSE
FUNCTION- This command closes one or more open files or devices. CLOSE without any argument closes all open files and devices.
Syntax- CLOSE # FILENUMBER, FILENUMBER n
EOF
FUNCTION- EOF test for the end of a file. EOF returns true if the end of the file has been reached.
Syntax- EOF (FILE NUMBER %)
File System Commands
1. CHDIR
FUNCTION- Changes the drive’s default directory.
Syntax- CHDIR PATHNAME$
2. MKDIR
FUNCTION- Creates a sub- directory.
Syntax- MKDIR PATHNAME$
3. RMDIR
FUNCTION- Removes a sub- directory.
Syntax- RMDIR PATHNAME$
4. FILES
FUNCTION- Displays the contents of the current directory, or a specified directory.
Syntax- FILES [FILESPEC$]
5. NAME
FUNCTION- Renames a file or directory.
Syntax- REN OLDSPEC$ AS NEWSPEC$
6. KILL
FUNCTION- Deletes a file from disk.
Syntax- KILL FILESPEC$
7. RUN
FUNCTION- Executes a current program or a specified program.
Syntax- REN [LINE NUMBER / FILE$]
8. SYSTEM
FUNCTION- Closes all open files and returns control to the operating system.
Syntax- CHDIR PATHNAME$
9. SHELL
FUNCTION- Suspends execution of a QBASIC program to run a DOS command or a batch file.
Syntax- SHELL COMMAND STRING$
1.
Program to create a sequential data file “STUDENT.DAT” to store students name, address and telephone no.
REM TO CREATE AN INPUT FILE
OPEN “STUDENT.DAT” FOR OUTPUT AS #1
CLS
DO
INPUT “STUDENTS NAME”; N$
INPUT “STUDENTS ADDRESS”; A$
INPUT “TEL NO”; T
WRITE #1, N$, A$, T
INPUT “WANT TO CONTINUE Y/N” ; CH$
LOOP WHILE CH$ =”Y” OR CH$ = “y”
CLOSE #1
END
2. Program to read and display all records from the file “STUDENT.DAT”.
REM TO CREATE AN OUTPUT FILE
OPEN “STUDENT.DAT” FOR INPUT AS #1
CLS
PRINT “NAME”, “ADDRESS”, “TEL NO”
DO WHILE NOT EOF (1)
INPUT #1, N$, A$, T
PRINT N$, A$, T
LOOP
CLOSE #1
END
3. Program to display only those records whose address is Kathmandu.
OPEN “STUDENT.DAT” FOR INPUT AS #1
CLS
PRINT “NAME”, “ADDRESS”, “TEL NO”
DO WHILE NOT EOF(1)
IF A$ = ”KATHMANDU” THEN
INPUT #1, N$, A$, T
PRINT N$, A$, T
END IF
LOOP
CLOSE #1
END
4. program to display only those records whose name is Riya and address is Kathmandu.
OPEN “STUDENT.DAT” FOR INPUT AS #1
CLS
PRINT “NAME”, “ADDRESS”, “TEL NO”
DO WHILE NOT EOF(1)
IF N$="RIYA" AND A$ = ”KATHMANDU” THEN
INPUT #1, N$, A$, T
PRINT N$, A$, T
END IF
LOOP
CLOSE #1
END
5. program to add more records to the file “STUDENTS.DAT”
OPEN “STUDENTS.DAT” FOR APPEND AS #1
CLS
DO
INPUT “STUDENTS NAME”; N$
INPUT “STUDENTS ADDRESS”; A$
INPUT “TEL NO”; T
WRITE #1, N$, A$, T
INPUT “WANT TO CONTINUE Y/N” ; CH$
LOOP WHILE CH$ =”Y” OR CH$ = “y”
CLOSE #1
END
Comments