Search Tools Links Login

The PROPER way to read and write to files

Visual Basic 6, or VB Classic

This is the PROPER, no BS way to read/write to files. Someone earlier today made a tutorial like this and it was alright but this is the easiest way. I just wanted to correct the newbies.. laterz..

Original Author: RyanConard


Read and Write Files

First, create a form and 2 command buttons. Name the buttons Command1 and Command2 (default names) or you change them but you must change the code as well.

Now, just copy the code below as it is into your project.

Private Sub Command1_Click()

Open "C:file.txt" For Append As #1

Write #1, "Hello"

Close #1

End Sub

Private Sub Command2_Click()

Dim Caption As String

Open "C:file.txt" For Input As #1

Input #1, Caption

Me.Caption = Caption

Close #1

End Sub

There, all set. Now just test it and see what it does. It write the world "Hello" to a file, "file.txt" and then when you click Command2 it opens that same file, takes the word out and sets it as the form caption. This can be altered to do lots of other things.

More Information

You may notice the fact that there 3 types of file procedures: Append, Output and Input. These things mean:

Append: This will open the selected for for Append. Append, meaning an addition to. It will open a file and write information TO it instead of deleting the contents and re-writing the data.

Output: This is the same as above, but this will open a file for pure Output. It will take a file and delete whatever is in it (if anything) and put only the given data into the file. Use the Output and Append correctly or you might mess something up one day.

Input: This opens the selected for program Input. Input, meaning it takes the data in a file and (in)puts it in one of your programs variables. Then you can use that variable to divi out the data to controls and etc.. Look at how i used it above.

Notice how these things are given numbers, such as #1. #1-#3 are what you will see most often in professional code, but all this does is give certain data thats either being inputed, outputed, or appended an identification tag so things dont get messed up. Some program may have up 10 files opened at a single time, therefore that program would have #1-#10 in it's Opened files. Think of it as a cars license plate. If every car had the same license plate number and a cop was looking for a car with that number, the cop might get the wrong person because everyones the same. So if you named everything #1 then VB would have errors in assigning correct things to do. I'm not sure, but I believe VB will only allow you to go up to #20.

Heres some examples of multitasking

Dim WinIni As String

Open "C:WindowsWin.ini" For Input As #1

Open "C:WindowsWinIniCopy.ini" For Output As #2

Open App.Path & "log.txt" For Append As #3

Input #1, WinIni

Write #2, WinIni

Write #3, "Today: Copied the WinIni file!"

Close #1, #2, #3

Ok folks, there ya go.. Hope this helps! Now I gotta go and play GTA : Vice City. Cya! You dont have to vote.. After all, information is free!

About this post

Posted: 2003-06-01
By: ArchiveBot
Viewed: 109 times


Visual Basic 6


No attachments for this post

Loading Comments ...


No comments have been added for this post.

You must be logged in to make a comment.