Gw Temp

Menu

Tutorial - 'Text Files In Visual Basic' by DarkStorm

An item about Programming Languages posted on

Blurb

Making Text files in Visual Basic! Read up if you don't know how.

Body

=================================
Using Text files in Visual Basic
=================================
By Darkstorm

(Note: This is version two, with the horrific error in the last version
removed... sorry for any confusion :P
Incase i missed any of the errors (doubtful) any loops that are mean't
to go on until the END of a file should be...
DO UNTIL EOF)

A lot of people seem to find it hard to operate text files inside of visual basic, it is actually
quite an easy operation and can be used for all manner of useful things.

I.e. The caeser games used to store all of the model data in a text file so that with
a few easy modifications you could do all sorts of weird stuff like setting minus values
for the costs of buildings so that you would get payed to build them.

(For those interested the file was called (in Caeser 3) C3_Model.txt its in the directory
somewhere)

When a file is opened inside visual basic it is assigned a number:
I.e. Model Data could be #1, Health statistics #2, etc.

When you open a file in visual basic you must also tell the interpreter what you are opening the
file for.

------

Output:- This saves over any data currently in the file, or if the file does not exist creates
a new one.

Append:- This adds the data you put in the file on the end of what is already there,
or if the file does not exist creates a new one.

Input:- This is used when you wish to read the data that exists inside the file, so you
can display it or do whatever you want with it.

(There are more i think but i havn't finished learning VB yet and have not yet needed them)

The command to open a file goes like this...:
Open (File Address in speech marks) For (What you want to do with it i.e. Append) As #(The number you wish to assign it)


**************************
*Reading Data from a file*
**************************

If i wanted to open a file called 'Hello.txt' so that i could read the data onto the screen,
first i need to open it in input mode i would do this by:

Open (File address in speech marks here) For Input As #(the number you wish to assign it)

This open command is the same with all of the different methods, you would just substitute
the input for append, or Output.

In the example that i have given above the command line would look like this:
Open "C:\Hello.txt" For Input As #1

Now that the file is open i hear you asking me how to get the data out of it, well to start with you
need a variable suitable for the task. (I.e. for an address you might declare it as Dim varAddress As String)

Now that you have the variable here is the line of code you use to get a line of data out of the file:

Line Input #(file number here), (variable here)

In our case this would read:

Line Input #1, varAddress

This would read the first line of the text document and save it into the specified variable. Every time you
use the command it then moves down to the next line in the text file and reads that.

(When you have finished with a file use the command: Close #(FileNumber)' to close it)

Generally if you wish to read a file into variable storage you would run it something like this:

-----------
Dim varCounter as Integer 'This variable is used to count the length of document
Dim varTempStorage as String 'This is to temporarily store data while counting
Dim varFileData(1) as String 'Used to store the seperate lines of data, the size dosn't matter as it will be resized

Open "C:\Hello.txt" For Input As #1 'Open the file as file No 1

Do Until EOF(1) 'This tells the program to loop until the end of file one is reached
varCounter = varCounter + 1
Line Input #1, varTempStorage
Loop
Close #1 'So that we can reset the file to read the lines from the beginning of the file again

'At this point varCounter now has the number of lines in the file stored

Open "C:\Hello.txt" For Input as #1

ReDim varFileData(varCounter) as String (Re-Declares the Array to length of the file to store the lines of data in)

Do Until EOF(1) 'Loop until the end of file one
Line Input #1, varFileData(i) 'Stores the line of text into the position in the array marked by i
Loop
------------

That program stores every line in 'Hello.txt' into an array with the first line beginning at position 0 in the
array.

***********************
*Saving Data to a file*
***********************

There are two methods for this, Append and Outut i have already explained what each of them does.

They are both used in exactly the same way:

'This program opens the file we were using above and saves over it with the contents of a variable

Open "H:\Hello.txt" For Output as #1 'Opens the file and assigns it a number
Print #1, varTempStorage 'This saves over the file with the contents of the varTempStorage variable
Close #1 'Close the file again

The output command will always save over the file, and if the file does not exist then it will create
the file for you with the specified name and address.

The append command would do exactly the same and be used in the same way but would add the data stored
in the variable onto the end of the file.


*******************
*Working With Text*
*******************

This section is entirely optional, it mainly goves some methods for working with the text data that you get from a file.

The Split command:- This is a very useful command as it splits up a line of text into seperate components and
then drops them into an array for you.

First of all you need what is known as a dynamic array, when you declare it you must not specify a length for it
I.e. Dim varArray()

You need a line of text stored in a variable that will be split up, in this case i will have this:
Dim varInfo as String
varInfo = "DarkStorm 16 22/05/1987"

The data above is name, age, and DOB; i may want to split this information up so that i can seperately
do 'stuff' with the individual bits of information. The command is:

(Array here) = Split((variable to split up here),(the data you wish to use to identify a new piece of data))

It may look complex but its not, in the example i am giving i would make the program like this:

-----------
Dim varArray() as String, varInfo as String
varInfo = "DarkStorm 16 22/05/1987"

varArray() = Split(varInfo, " ") 'the last part says i want to use a space to show when to split the line
-----------

The end result for this would be an array which in positon (0) had "DarkStorm", in (1) "16", and in (2) "22/05/1987".

Another useful command for if you have an array which needs joining back together is the join command:

(variable name here) = Join((array to join up), (What to put between the array items)

If i wanted to join my above array back together again i would use:

----------
varInfo = Join(varArray(), " ")
----------

This would join the array back together using a space in between each seperate component.


Well there it is, i hope it was useful, if anyone does find it useful, email me or leave feedback
and i will (maybe) extend the working with text section to include more commands and examples to store
data using text.


~DarkStorm