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. Balchen

Before you start

You 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:
  • Integer
  • Long
  • Double
  • Single
There are more, but these are the ones you are most likely to use in your applications. For more info on the specific types, look in your Visual Basic help file.

Writing Data To a File

There 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 wrote

If 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
-42

I find it to have no value, but this is how it works.

Reading Data From a File

If 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:
  • If the data is a string, it will read until it finds a comma or a line feed, and return the string
  • If the data is numeric, it will read until it finds a line feed, and return the number as the appropiate type
Now, in order to read a number, you do this:

   Dim Data As Integer

   Open "C:\Windows\My File.txt" For Input As #1
      Input #1, Data
      Debug.Print Data
   Close #1

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 Section

In 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.