Qbasic File Handling [NOTES]

 

Qbasic File Handling [Notes]

 

1. What is file handling?

Ans: File handling is a process to create a data file, write data to the data file, and read data from the specified data file.

2. Define File.

   - Ans: A file is a collection of different data, information, or instructions stored in secondary storage devices under a unique file name. In QBASIC, there are two types of files:

     - Data file

     - Program file

3. Differentiate between a data file and program file.

   - Ans:

     - Data file: Contains data such as name, address, phone, etc., required for data processing. It stores related data in rows known as records. Data files are stored separately from program files and provide data to program files.

     - Program file: Contains instructions written in a computer language for data processing under a unique file name. It contains instructions used to manipulate data and produce output. Program files in QBASIC have a .BAS extension.

 

4. Write some advantages of the data file.

   Ans: Advantages of data files include:

     - Data stored in data files can be reused.

     - Single data files can be used by different programs.

     - Provides flexibility to recover saved data when required.

     - Data files can be created, modified, or deleted.

5. Explain different types of data files used in QBASIC

   Ans:

     1. Sequential data file: Performs reading and writing operations sequentially from the beginning to the end of the file. Records need to be accessed in the same order as they are stored.

     2. Random access data file: Performs reading and writing operations randomly in any order. Records can be accessed directly in any order.

6. What is file mode? List Different file modes in QBASIC.

   -Ans: File mode refers to the purpose of opening data files. Common modes of opening data files in QBASIC include:

     - OUTPUT: Creates a new sequential file and writes data in it from the beginning.

     - INPUT: Reads/displays data from existing data files.

     - APPEND: Adds more records to an existing data file from the end.

 

7. Write down the purpose of the following statements and functions:

   - Ans:

     - OPEN statement: Used to open sequential data files for writing or reading data using Output, Input, or Append mode.

     - WRITE # statement: Sends or stores one or more data items to the specified file using Output or

        Append mode.

     - CLOSE and CLOSE # statement: Used to close opened data files.

     - INPUT # statement: Reads data from an existing sequential data file and stores them in a variable.

     - LINE INPUT # Statement: Reads an entire line or maximum 255 characters from the keyboard or   

      sequential file.

     - EOF() function: Checks the end of file marker while reading data from an open sequential file.

     - INPUT $ statement: Returns a specified number of characters from a data file.

     - FILES statement: Displays the list of file names in the current directory.

     - NAME ... AS ... statement: Renames a file.

     - KILL statement: Deletes a file from the specified disk or directory.

     - MKDIR statement: Creates a new directory.

     - CHDIR statement: Changes the default directory on a specified drive location.

     - RMDIR statement: Deletes a directory from a disk.


The syntaxes for different modes of file handling in QBASIC:

1. OPEN Statement:

   - Syntax: `OPEN "FILENAME.DAT" FOR <MODE> AS #FILE NUMBER`

   - Definition: The OPEN statement is used to open a sequential file for one of the three possible operations (Input, Output, Append).

   - Example: `OPEN "EMPLOYEES.DAT" FOR INPUT AS #1`

2. INPUT # Statement:

   - Syntax: `INPUT #File number, variable list`

   - Definition: Allows reading data from an opened sequential data file.

   - Example: `INPUT #1, Name$, Age, Salary`

3. CHDIR Statement:

   - Syntax: `CHDIR "location name"`

   - Definition: Changes the current directory.

   - Example: `CHDIR "C:\QBASIC"`

4. MKDIR Statement:

   - Syntax: `MKDIR "path"`

   - Definition: Creates a new directory.

   - Example: `MKDIR "C:\QBASIC\DATA"`

5. RMDIR Statement:

   - Syntax: `RMDIR "Path"`

   - Definition: Removes a directory.

   - Example: `RMDIR "C:\QBASIC\DATA"`

6. KILL Statement:

   - Syntax: `KILL "File name"`

   - Definition: Deletes files from the specified location.

   - Example: `KILL "EMPLOYEES.DAT"`

 

7. LINE INPUT # Statement:

   - Syntax: `LINE INPUT #File number, Variable List`

   - Definition: Used only for string input. Reads the entire line or maximum 255 characters from the keyboard or sequential file.

   - Example: `LINE INPUT #1, DataLine$`

8. PRINT # Statement:

   - Syntax: `PRINT #File number, Variable List`

   - Definition: Used to send values to the data file.

   - Example: `PRINT #1, Name$, Age, Salary`

9. INPUT$() Function:

   - Syntax: `INPUT$(N, #File number)`

   - Definition: Reads specified numbers of characters from a data file.

   - Example: `InputData$ = INPUT$(20, #1)`

10. FILES Statement:

    - Syntax: `FILES(Specs)`

    - Definition: Displays the list of files.

    - Example: `FILES "*.DAT"`

11. EOF() Function:

    - Syntax: `EOF(File Number)`

    - Definition: Used to find out the end of the data file which occurs after the last record.

    - Example: `EOF(1)`

12. NAME AS Statement:

    - Syntax: `NAME "old name" AS "new name"`

    - Definition: Renames a file or directory on a disk.

    - Example: `NAME "EMPLOYEES.DAT" AS "EMPLOYEE_INFO.DAT"`

 

13. WRITE# Statement:

    - Syntax: `WRITE #File number, Variable list`

    - Definition: Sends one or more data items to the specified file (to write data to a sequential file).

    - Example: `WRITE #1, Name$, Age, Salary`

 

14. CLOSE# Statement:

    - Syntax: `CLOSE #File number`

    - Definition: Closes the file after Input/Output operation.

    - Example: `CLOSE #1`


 


QBASIC File handling Questions

1.  Write a QBASIC program to read data from a sequential data file called "EMPLOYEES.DAT". The file contains employee information such as name, address, age, and salary. Display all the records stored in the file.

OPEN "EMPLOYEES.DAT" FOR INPUT AS #1

WHILE NOT EOF(1)

INPUT #1, Name$, Address$, Age, Salary

PRINT Name$, Address$, Age, Salary

WEND

CLOSE #1

2. Create a program in QBASIC that opens a data file named "STUDENTS.DAT". The file contains records with fields for student name, age, class, and grade. Display all the records stored in the file.

OPEN "STUDENTS.DAT" FOR INPUT AS #1

WHILE NOT EOF(1)

INPUT #1, Name$, Age, Class, Grade$

PRINT Name$, Age, Class, Grade$

WEND

CLOSE #1

3. Develop a QBASIC program that reads data from a sequential data file called "INVENTORY.DAT". The file contains information about items in stock, including item name, quantity, and price. Display all the records stored in the file.

OPEN "INVENTORY.DAT" FOR INPUT AS #1

WHILE NOT EOF(1)

INPUT #1, ItemName$, Quantity, Price

PRINT ItemName$, Quantity, Price

WEND

CLOSE #1

 

4. Write a QBASIC program to open a sequential data file named "CUSTOMERS.DAT". The file contains customer information such as name, address, phone number, and email. Display all the records stored in the file.

OPEN "CUSTOMERS.DAT" FOR INPUT AS #1

WHILE NOT EOF(1)

INPUT #1, Name$, Address$, Phone$, Email$

PRINT Name$, Address$, Phone$, Email$

WEND

CLOSE #1

 

5. Design a QBASIC program that reads data from a data file called "PRODUCTS.DAT". The file contains product details including name, description, price, and quantity. Display all the records stored in the file.

OPEN "PRODUCTS.DAT" FOR INPUT AS #1

WHILE NOT EOF(1)

INPUT #1, Name$, Description$, Price, Quantity

PRINT Name$, Description$, Price, Quantity

WEND

CLOSE #1

6. Create a program in QBASIC that opens a data file named "SALES.DAT". The file contains sales records with fields for product name, quantity sold, and total amount. Display all the records stored in the file.

OPEN "SALES.DAT" FOR INPUT AS #1

WHILE NOT EOF(1)

INPUT #1, ProductName$, QuantitySold, TotalAmount

PRINT ProductName$, QuantitySold, TotalAmount

WEND

CLOSE #1

 

7. Develop a QBASIC program to read data from a sequential data file named "TRANSACTIONS.DAT". The file contains transaction details such as date, time, customer name, and amount. Display all the records stored in the file.

OPEN "TRANSACTIONS.DAT" FOR INPUT AS #1

WHILE NOT EOF(1)

INPUT #1, Date$, Time$, CustomerName$, Amount

PRINT Date$, Time$, CustomerName$, Amount

WEND

CLOSE #1

 

8. Write a QBASIC program to open a sequential data file called "ORDERS.DAT". The file contains order information including order number, customer name, product name, and quantity. Display all the records stored in the file.

OPEN "ORDERS.DAT" FOR INPUT AS #1

WHILE NOT EOF(1)

INPUT #1, OrderNumber, CustomerName$, ProductName$, Quantity

PRINT OrderNumber, CustomerName$, ProductName$, Quantity

WEND

CLOSE #1

9. Design a QBASIC program that reads data from a data file named "INVOICES.DAT". The file contains invoice details such as invoice number, customer name, date, and total amount. Display all the records stored in the file.

OPEN "INVOICES.DAT" FOR INPUT AS #1

WHILE NOT EOF(1)

INPUT #1, InvoiceNumber, CustomerName$, Date$, TotalAmount

PRINT InvoiceNumber, CustomerName$, Date$, TotalAmount

WEND

CLOSE #1

10. Create a program in QBASIC that opens a data file named "EXPENSES.DAT". The file contains expense records with fields for date, description, category, and amount. Display all the records stored in the file.

OPEN "EXPENSES.DAT" FOR INPUT AS #1

WHILE NOT EOF(1)

INPUT #1, Date$, Description$, Category$, Amount

PRINT Date$, Description$, Category$, Amount

WEND

CLOSE #1


Post a Comment

1 Comments