File Handling


A file is a collection of bytes stored on the disk with a given name (called as filename). Every development tool provides access to these files on the disk. In this chapter we will understand how to access and manipulate files using Visual Basic.

There are three special controls, called as File controls, which deal with files and directories. We will also understand how to use these controls in this chapter.

File handling
The following are three important steps in handling a file.
Image result for file handling
  • Opening the file
  • Processing the file, i.e. either reading the content of the file or writing the required data into file or both.
  • Closing the file


Defining new terms


  • Record: one logical section of a file that holds a related set of data. If the file contains Student information, a record would hold the information on one student: name, address, studentID, etc. If there are 5,000 students registered, the file contains 5,000 records.
  • Field: part of a record that defines a specific information. In the Student record, FirstName, LastName, StudentID, are fields. The field is the lowest element in the file. Even if the information consists of one character, Sex is M or F, it is still considered a separate field. The field is the equivalent of the variable - we call it a variable when it is used to store data in memory and call it a field when it stores in a file.
  • I/O: stands for Input/Output. Whenever you work with a file you have to have ways of reading data from the file (that'sInput) and ways of writing data to the file (that's Output). I/O operations consist of all those commands that let you read and write files.

Types of files

There are basically three types of files you can work with:

  • Sequential file: this is a file where all the information is written in order from the beginning to the end. To access a given record you have to read all the records stored before it. It is in fact like listening to a tape - you can go forward or back but you can't jump directly to a specific song on the tape. In fact, in the old days, magnetic tape was the most commonly used medium to store data and all files were organized this way. Now, it is still useful when there is a small amount of data to store, a file of application settings, for example. It can even be of use when there is a large amount of data to be stored, provided it all has to be processed at one time, eg: a file of invoices to produce a statement at month-end.
  • Random file: a file where all records are accessible individually. It is like a CD where you can jump to any track. This is useful when there is a large quantity of data to store and it has to be available quickly: you have to know if a part is in stock for a customer who is on the phone; the program doesn't have time to search through 10,000 records individually to locate the correct one. This method of storage became popular when hard-disk drives were developed.
  • Binary file: this is a special, compacted form of the random file. Data is stored at the byte level and you can read and write individual bytes to the file. This makes the file access very fast and efficient. We won't be covering this type of file in these exercises. If you need to find out more about it, go to the VB Reference Manual.

Access Mode

For Mode in the Open statement indicates how the file will be used. There are five access modes:
  • Input: open for sequential input; the file will be read sequentially starting at the beginning.
  • Output: open for sequential output; records will be written sequentially starting at the beginning; if the file does not exist, it is created; if it does exist, it is overwritten.
  • Random: open for random read and write; any specific record can be accessed.
  • Append: sequential output to the end of an existing file; if the file does not exist it is created; it does not overwrite the file.
  • Binary: open for binary read and write; access is at byte level.
If access mode is not specified in the Open statement, For Random is used by default.