Handling Files in Visual Basic
The majority of Windows applications handle files. From Microsoft Word to Windows Explorer to Greasy Joe's Easy Accounts package.
But how do you do this in Visual Basic? Via the Open statement.
In this article, I'll be covering everything you need to know - from start to finish. Perhaps even from open to close, if I'm feeling funny.
Don't forget that I'd love to hear what you think about this article. Whether it's likes or gripes, post it on the bulletin board.
But for now, let's get started.
Before we delve into writing and reading files, let's ask ourselves a question; why would I want to write to a file?
Well, programmers often want to write data to a disk or retrieve data from it. And that data can be anything - from settings to plain text to pictures. Heck, you could even create your own mini word processor!
But don't confuse this with databases. If you're interested in accessing databases using Visual Basic, you'd better head down to Karl Moore's tutorial.
Now, we access files using the Open statement.
Here's an example:
Open "c:\windows\faq.txt" For Input As #1
Don't worry about the syntax for now, you'll get used to it. This example opens the file faq.txt for input (file reading). The file is referred to by the number 1.
Before we dive into the intricacies of using the Open statement, let's take a peek at three different modes available to us when accessing files:
- Input, Output and Append
We'll cover each of these separately.