In this chapter, you will learn the following:
• Introduction to Data Files
• Sequential Disk Data Files
• Writing a Text File
• Ramdom Access Data Files
• The frmRandom Class Code
In this chapter, we'll explore how you can save data to files stored on your computer's disk drive. We'll also discuss the various types of data files thatyou can use and some of the available file options. Further, I'll show you just how easy reading and writing Visual Basic .NET disk data files can be, especially in light of all the ready-to-use tools (for example, SaveFileDialog) that Visual Basic .NET makes available to you. Finally, I'll also use this chapter as an opportunity to introduce you to creating menus with Visual Basic .NET. Before wedo these things, however, we need to get under the hood and make sure that we understand what disk data files are all about.
Introduction to Data Files
Computers would not be the pervasive tools that they are today if there were no permanent way to save the information contained in the computer. First of all, we would be limited to amount of data that could be stored in memory. Thatrestriction alone would severely limit the computer's usefulness as a business tool. Second, even if we could store all our information needs in memory, shutting the computer down would mean re-entering all that information again each time we wanted to use it. Clearly, there has always been a need for a means of permanent storage of computer data.
When microcomputers first came on the scene in the 1970s,the mass storage device was a cassette tape recorder! I can remember turning on the computer and having the lonely prompt of a bare-bones operating system staring at me. I would then issue a cryptic command telling the computer to load Tom Pittman's Tiny Basic from the cassette tape recorder, press Enter, and go fix lunch. If I made enough sandwiches and the computer was having a good day, when Ireturned, the 4-kilobyte BASIC interpreter would be ready for me to start programming. Although there were ways to save data to the tape recorder, it was so unreliable and slow, I rarely used such data files.
Then Northstar Computers came out with an operating system that supported 90-kilobyte disk drives using 5.25'' floppy diskettes. I thought I had died and gone to heaven! The drives wereextremely fast (remember the frame of reference) and how could anyone ever need more than 90KB of storage? Indeed, I wrote an entire accounting system, including GL, AR, and AP, for my consulting business that fit on one 90KB diskette with room to spare!
I was so excited about the possibilities that I tried to convince one of my clients (a major insurance company) that these small computers wouldhave a major impact on the way their agents would conduct day-to-day business. Alas, they didn't believe it and commented that "…these computers will never do more than play games." Well, in fairness to them, that was back in the late 1970s and there were some serious shortcomings, especially when it came to mass storage devices.
My, oh my, how things have changed. Now you can buy a mega-munch ofdisk storage for less than a couple of dollars per gigabyte. The availability of relatively inexpensive disk storage has played a major role in the acceptance of microcomputers in the business community. With that in mind, let's see how to use some of that storage space.
Sequential Disk Data Files
Data written to a sequential file follows the cassette tape recorder model mentioned earlier.That is, data is usually written starting at the beginning of the file (BOF) and copied byte-by-byte until all the data has been written. At that point, an end of file (EOF) marker is written and the file is closed. The sequential disk data file layout can be seen in Figure 23.1.
Figure 23.1. The data layout for a sequential disk data file.
The File Pointer
Try to visualize what...