Using Files: Section III
In this section, we will look at reading and writing other types of data when strings. The problems that this causes in international Windows will also be covered along with ways to guard yourself.
Author: Jens G. BalchenBefore you startYou will need to be familiar with the numeric data types Integer, Long, Double, and Single.You should also read the previous sections to see if they cover something you don't know and get a feel for the stuff. What Other Data Types Are There?Apart from Strings, Visual Basic has 14 basic data types. We will look at the numeric data types in this section:
Writing Data To a FileThere is no difference between writing strings and writing numeric data to a file. In both cases, the Print command from Section I will do just fine. The only different will appear in the file, and you should be aware of this:If you write a string to a file, the file will look like this This is the string that you wroteIf you write numeric data, the file will look this 42(42 represents the number you wrote). Notice the space in front of the number. This is intentional -- Visual Basic reserves an empty space for a - sign, so that numbers will be left-aligned. Negative numbers will appear like this 42 -42I find it to have no value, but this is how it works. Reading Data From a FileIf you want to read numeric data, you cannot use the Line Input command. It is designed to read entire lines of text, and returns it as a string. To read number, you use the Input command. It has the same syntax as Line Input, but it will do the following:
Dim Data As Integer
The code doesn't differ much from the code to read strings. If you have a file with numeric content, you will see it displayed in the Debug Window. If not, you will get a 0. If you want to read a Double, not an Integer, you just replace Dim Data As Integer with Dim Data As Double Basically, you use Input for numbers and Line Input for strings. Next SectionIn the next section, we will discuss other mehods of reading files, how to read binary files, and how to use user defined types (UDTs) in files. |
||
Editor: Jens G. Balchen Last update: 2024-12-30 Copyright 1995-2024 VBI |