Answer: – 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.
The following are three important steps in handling a file.
- 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
File access types – Depending upon the requirement you can use any of the three different file access types:
Sequential For reading and writing text files in continuous blocks.
Random For reading and writing text or binary files structured as fixed-length records.
Binary For reading and writing arbitrarily structured files.
Opening the file using Open statement – A file is opened using OPEN statement in Visual Basic. At the time of opening a file, you have to specify the following.
- Name of the file to be opened
- The mode in which file is to be opened. The mode specifies which operations are allowed on the file.
- File number. Each open file contains a file number. This file number is used to access the file once the file is opened. The file number must be unique.
Open pathname for mode [Access access] [lock] As [#] filenumber [Len=reclength]
Option Meaning Pathname Name of the file to be opened. Mode Specifies the mode in which file is to be opened. Valid modes: Append, Input, Output, Binary and Random. If unspecified then Random is taken. Access Specifies the operations that are permitted on the open file. Valid values: Read, Write, or ReadWrite. Lock Specifies the operations restricted on the file opened by other users. Value values: Shared, Lock Read, Lock Write, and Lock Read Write. Filenumber A number in the range 1 to 511. This number must be unique among open files. Use FreeFile function to obtain the next available number. RecLength Specifies the size of each record in random files. It should be <= 32767. For sequential files, this is the number of characters buffered. This is ignored, if mode is Binary. If the file is not existing then a new file with the given name is created in Append, Binary, Output and Random modes. Examples: To open TEST.TXT file in input mode: Open “TEST.TXT” for input as #1 To open NUMBER.DAT file in Binary mode: Open “NUMBER.DAT” for binary access write as #1 To open STUDENTS.DAT in random mode with a record length of 10: Open “STUDENTS.DAT” for random as #1 len = 10 To get next available file number and then use it: ‘FreeFile function returns the number that can be used as the file ‘number while opening the file Fn = FreeFile Open “TEST.TXT” for input as #fn Functions related to files The following are the functions that are used with files. Function Meaning Dir Returns the name of the file that matches the given name. If file is not existing then it returns “” (null string). FileLen Returns the length of the file in bytes. LOF Returns the length of an open file in bytes. EOF Returns true, if the specified file has reached end-of-file marker. FreeFile Returns the next available file number. Seek Sets or returns the position at which file pointer is currently positioned. For random files it returns the number of records read or written so far. Filecopy Copies the given source file to target file. GetAttr Returns the attributes of the given path. SetAttr Changes the attributes of the specified file to the given attributes. FileDateTime Returns the date and time when file was last modified or created. Loc Returns the current position of file pointer of an open file. Table 10.1: Functions related to file handling. Example: To find out the length of file CHARS.TXT: fl = FileLen(“c:\vb60\chars.txt”) To find out whether file with the file number 1 has reached end-of-file: if EOF(1) then … end if To check whether STUDENTS.DAT file is existing or not: If dir(“students.dat”) = “” then MsgBox “File students.dat is missing” Else Process the file End if Statement related to file Input and output The following statements are used to perform input or output to file and other operations such as opening and closing. Statement Meaning Close Closes an open file Get Read a record from the given position of the specified file. Input( ) Returns the specified number of characters from the given file. Input # Reads data into specified list of variables from the given file. Line Input # Reads a complete line from the given file. Open Opens the given file in the specified mode. Print # Prints the specified data to the given file. Put Writes a record to the given position of the specified file. Write # Writes the specified data to the given file.
Syntax Errors – Syntax errors are those that appear while you write code. Visual Basic checks your code as you type it in the Code Editor window and alerts you if you make a mistake, such as misspelling a word or using a language element improperly. Syntax errors are the most common type of errors. You can fix them easily in the coding environment as soon as they occur.
Run-Time Errors – Run-time errors are those that appear only after you compile and run your code. These involve code that may appear to be correct in that it has no syntax errors, but that will not execute. For example, you might correctly write a line of code to open a file. But if the file is corrupted, the application cannot carry out the open function, and it stops running. You can fix most run-time errors by rewriting the faulty code, and then recompiling and rerunning it.
Logic Errors – Logic errors are those that appear once the application is in use. They are most often unwanted or unexpected results in response to user actions. For example, a mistyped key or other outside influence might cause your application to stop working within expected parameters, or altogether. Logic errors are generally the hardest type to fix, since it is not always clear where they originate.