Note on File Handling

  • Note
  • Things to remember
  • Videos
  • Exercise
  • Quiz

Introduction

Data are used in all the programs for retrieving information. In QBASIC you can store data in a separate file called a data file. Hence there are two types of files in data processing. They are:

  • Program file :A program file contains a set of instructions that are needed for data processing. A program file has BAS extention.
  • Date file : A data file contains only data that are required during data processing. In a data file related of a person or any thing are stored in a row known as a record.

File Handling

It is a process to create data file,write data to the data file and read data from it.

Types of File Handling

  • Sequential Data File : It is a file that should be accessed in a sequential manner starting at the beginning of the data block and process ending in order until and end of data.
  • Random Data File: It is a file which a key is used to point its appropriate record stored on a disk.

Some useful commands with their function and syntax

Files: This command is used to display all the files of current drive. Syntax: Files [File specification]

Shell: This command is used to go to DOS prompt temporarily.

NAME: This command is used to change the old file into new file. Syntax: Name " old file name" AS "New file name"

Kill: This command is used to remove the file from the disk. Syntax: Kill [ File Specification ]

MKDIR: This command is used to create a directory in a specific disk Syntax: MKDIR "path"

CHDIR: This command is used to change the directory. Syntax: CHDIR " path"

RMDIR: This command is used to remove the specific directory. Syntax: RMDIR "path"

System: This command is used to close the Q-BASIC program.

OPEN: This statement is used to open the file.

WRITE#: This statement is used to place the data into data file.

NPUT#: This statement is used to read each data and stores to corresponding variable.

LINE INPUT#: This statement reads entire line having maximum 255 characters and stores to a single string variable.

EOF ( ) : This statement is used to end a file. Syntax: EOF ( File number)


Modes of operations of sequential access data file

You need to open a sequential access data file in order to use it. A sequential data file can be opened in one of three modes.

Types of Mode:

  • OUTPUT mode : A file is opened in output mode to write data to the file. When a file is opened in output mode BASIC always creates a new file. If a file with the name already exists then it will overwrite without warning.
  • INPUT mode : A file is opened in input mode to read data from the file. The file must exist to open it in input mode otherwise error message "file not found" is displayed.
  • APPEND mode : A file is opened in append mode to add data to an existing file. New data are added to the end of existing file. It is recommended mode for writing data to a file unless there is specific reason to create new.

  • File Handling is a process to create data file,write data to the data file and read data from it.
  • There are two types of file handling. They are Sequential Data File and Random Data File.
  • Files, shell, name, kill etc. are some useful commands with their function and syntax.
.

Very Short Questions

A file that contains a set of instructions that are needed for data processing is known as program and file that contains only data required during data processing is known as data file.

A sequential access data file is a file that should be accessed in a sequential manner starting at the beginning of the data block and process ending in order until and end of data.

OPEN"Library.dat" FOR OUTPUT AS #1
CLS
INPUT"Enter book number";B
INPUT"Enter book name";A$
WRITE #1,B,A$
CLOSE #1
END


OPEN"STD.dat" FOR OUTPUT AS #1
CLS
TOP:
INPUT" Enter your name";N$
INPUT"Enter your address";Ad$
INPUT"Enter your telephone number"; T
WRITE #1,N$,Ad$,T
INPUT"Do you want to add more records (Y/N)";A$
IF A$="Y" THEN GOTO TOP:
CLOSE #1
END

OPEN"Std.dat" FOR INPUT AS #1
CLS
DO WHILE NOT EOF(1)
INPUT#1,N$,C,R,
PRINT N$,C,R
LOOP
CLOSE#1
END

OPEN"student.dat"FOR APPEND AS #1
CLS
TOP:
INPUT"Enter your name";N$
INPUT" Enter your marks in English";E
INPUT"Enter your marks in Nepali";N
INPUT"Enter your marks in Computer";C
WRITE#1,N$,E,N,C
INPUT"To add more records (Y/N)";A$
IF A$="Y" THEN GOTO TOP:
CLOSE #1
END

OPEN" Employee.dat" FOR INPUT AS #1
CLS
DO WHILE NOT EOF ( 1)
INPUT #1,N$,D$,P$,S
If S>= 5000 AND S<= 1000 THEN
PRINT N$,D$,P$,S
END IF
CLOSE#1
END

0%
  • A process to create data file, write data to the data file and read data from it is called:

    file handling
    file hanging
    computer handling
    folder handling
  • Which one is not a type of mode?

    Output Mode
    Append Mode
    Handling Mode
    Input Mode
  • Which command is used to create a directory in a specific disk?

    RMDIR
    MKDIR
    CHDIR
    KILL
  • Which command is used to close the Q-BASIC program?

    INPUT
    RMDIR
    KILL
    System
  • Which one of the statement is used to end the file?

    CHDIR
    LINE INPUT
    EOF
    KILL
  • Which one of the following is a process to create data file,write data to the data file and read data from it?

    File receivinf
    File handling
    File copying
    File transfering
  • When a file is oppened in output mode BASIC always:

    deletes previous file
    displays an error message
    edits previous file
    creates a new file
  • The file must exits to open it in input mode otherwise:

    error messege will display
    file becomes hidden
    new file is created
    previous file is deleted
  • A file is opened in apend mode to:

    add data to an existing file
    to delete previous file
    to create a new file
    to make file hidden
  • A file is opened in output mode to:

    to delete previous file
    to create a new file
    to open hidden file
    write data to a file
  • You scored /10


    Take test again

DISCUSSIONS ABOUT THIS NOTE

You must login to reply

Forum Time Replies Report
ishmu

programming question


You must login to reply