File Input Output
![Visual Basic 6, or VB Classic Visual Basic 6, or VB Classic](https://www.fortypoundhead.com//assets/img/icons/v7/vb6.png)
File Input Output Append
Original Author: Tony G
Code
By Alex Allan In, Out, First off, let's take a look at the file modes input, output and But when should you use each mode? Well, it depends on what you Top Tip: You may hear these file modes being referred So, for example, to open a file for output, you'd use the That's all fine and dandy, but how do you use each mode To write to a file we use (Output and Append To read from a file we use (Input only): This probably looks completely confusing at the moment, so let's Let's imagine you've opened a file for output, like ...we now want to put information into this file using the Print This inserts the information you pass it direct to the file in If you'd opened a file for Input, like this... ...you can read information from the file, like This reads information from the file in #1 and puts it into your Let's use an example - it's easier to explain that way! Building a Let's build a sample to demonstrate accessing files: Open Visual Basic and double-click on "Standard EXE" You should be left with a blank form. So far, it should look something like this: Now, double-click on Read and insert the following Read the comments (anything prefixed by an apostrophe). Next, double-click on Write and insert the following Once again, read all the comments. Do you understand what's Hit F5 to run your program! Congratulations! You've just created a simple text editor. The Didn't I You may have noticed a few things in my code that I haven't told - This is the same as Input but it reads the whole line instead - This outputs a true or false value, depending on whether it has - This closes the file. It allows other files to open this And if you'll be getting real friendly with files in Visual This is similar to Print but it writes "screen formatted data" The LOC function returns the current read/write position within The LOF function gives you the length of the file This will give you next available file number. Example: This is the same as Input and Line Input except it has no limits Why not have a play around and see what these do? Go on, have a That's about it for the Input, Output and Append modes.
Handling Files in Visual Basic
Shake It All About
append. You use these three types to read or write plain text - such
as that found in .txt, .bat and .ini files.
want to do. Use the following list to help you decide...
write information into it.
appends (adds to) an existing file.
to as 'sequential files'. That's 'cause once you have read or
written to a line, you can't go back to it unless you close and
re-open. In other words, the modes are one way ÔÇô sequential.
Open statement like this:Open "c:windowsfaq.txt" For Output As #1
face="verdana, arial, helvetica" size=-1>
after you've opened the file?
only):Print #filenumber, expression
face="verdana, arial, helvetica" size=-1>
Input #filenumber, variablelist
face="verdana, arial, helvetica" size=-1>
figure out what it all means.
this...Open "c:groovymyfile.txt" For Output As #1
face="verdana, arial, helvetica" size=-1>
statement, like this:Print #1, "Hello World!"
face="verdana, arial, helvetica" size=-1>
#1.
Open "c:groovymyotherfile.txt" For Input As #1
face="verdana, arial, helvetica" size=-1>
this...Input #1, MyVariableName
face="verdana, arial, helvetica" size=-1>
variable.
Sample
to Read and the second to Write.
src="http://home.iprimus.com.au/ganino/image2.gif"
width=228 border=1>
code:Private Sub Command1_Click()
face="verdana, arial, helvetica" size=-1>
'Outline: - Asks the user for a file.
' - Reads all the data into Text1.
Dim FilePath As String
Dim Data As String
FilePath = InputBox("Enter the path for a text file", "The Two R's", _
"C:WINDOWSWINNEWS.TXT")
'Asks the user for some input via an input box.
Open FilePath For Input As #1
'Opens the file given by the user.
Do Until EOF(1)
'Does this loop until End Of File(EOF) for file number 1.
Line Input #1, Data
'Read one line and puts it into the varible Data.
Text1.Text = Text1.Text & vbCrLf & Data
'Adds the read line into Text1.
MsgBox EOF(1)
Loop
Close #1
'Closes this file.
End Sub
code:Private Sub Command2_Click()
face="verdana, arial, helvetica" size=-1>
'Outline: - Asks the user for a file.
' - Writes it all to a the file.
Dim FilePath As String
FilePath = InputBox("Enter the path for a text file", _
"The Two R's")
'Asks the user for some input via an input box.
Open FilePath For Output As #1
'Opens the file given by the user (for Output).
Print #1, Text1.Text
'Writes the data into the file number #1
Close #1
End Sub
happening?
Write button performs a simple 'Save As' whilst the Read button is
the equivalent of 'Open'.
Mention Those?
you about. Let's take a peek at a few geeky code words...Line Input #filenumber, MyVariableName
face="verdana, arial, helvetica" size=-1>
of stopping at a comma (which can be useful - sometimes)EOF(#filenumber)
face="verdana, arial, helvetica" size=-1>
hit the 'end' of the file. In my code, I used it in the DoLoop for
Read. When EOF=True, the loop ends.Close #filenumber
face="verdana, arial, helvetica" size=-1>
file.
Basic, here are a few other file writing functions you may be
interested in:Write #filenumber, outputlist
face="verdana, arial, helvetica" size=-1>
instead of raw data to a file. This means that values (numbers) have
hashes ("#") put around them and strings have quote marks put around
them. This makes the text less human readable but easier to read for
your programs.LOC(#filenumber)
size=-1>
an open file.LOF(#filenumber)
face="verdana, arial, helvetica" size=-1>
open.FreeFile
MyFileNumber = FreeFile
face="verdana, arial, helvetica" size=-1>
Open "c:windowsfaq.txt" For Input as #MyFileNumberInput$(number_of_chars_to_return, #filenumber)
face="verdana, arial, helvetica" size=-1>
(except the ones you set in number_of_chars_to_return). By using LOF
- this can be used to get the whole file. We could replace all the
stuff in the DoLoop in the Read button, with:Text1.Text = Input$(LOF(1),#1)
face="verdana, arial, helvetica" size=-1>
go!
Loading Comments ...
Comments
No comments have been added for this post.
You must be logged in to make a comment.