Writing to and reading from files is easy and useful. First you must open the file. Opening the file looks like this:
Open "C:\File1.txt" For Output As #1
Obviously Open, opens the file. The "C:\File1.txt" is the actual file that it is opening. This paticular example opens "C:\File1.txt" For Output. Output means that you will be able to write something to the file. In a minute we will discuss how to read the file too. Finally, we have an As #1. This is the filename number that will refer to file "C:\File1.txt". We opened this file as #1 (although we could have opened it as #2, #8, or even #21.) The next statement we will learn is the easy Print statement. This will print text to the file. It looks like this:
Print #1, "Hello World!"
This would print "Hello World!" (minus the quotation marks) to file #1. If we had opened the file as #15 then we would need to replace #1 with #15. Please take notice of the comma after #1. This is important because it seperates the file number, from the text to be printed to the file. You do not have to user a literal string as in the example above, you could also use a string variable. As a matter of fact, you could also use a variable of type Integer or even Long. In other words, you do not have to use a literal string or a variable of type string.
Be sure to always close your file when you are finished with it. You close a file by using the Close statement.
Close #1
Using common sense, replace #1 with the number of whatever file you are closing. The code, in one piece, looks like this:
Open "C:\File1.txt" For Output As #1
Print #1, "Hello World!"
Close #1
Before we discuss how to read files, it should be mentioned that opening a file for Output will not retain what was already in that file. It doesn't matter if you open the file and immediately close it; whatever was previously in the file is lost. By using the Append mode instead of the Output mode, you can retain the file. As the name implies, whatever you print to the file will be appended (or added) to the end of the file.
Reading files is just as easy as writing to them. You first open the file for Input:
Open "C:\File1.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, Addline
FinalText = FinalText + Addline
Loop
Close #1
FinalText and Addline are just strings. They could really be named anything, but for ease of understandability (and yes that is a word) those are the names they received. For the record though, you could use variables named Batman, Robin, Superman, Spiderman, or anything else your heart (or finger) desires. Let's first look at this statement:
Do While Not EOF(1)
This obviously starts a Do Loop, but it only executes until it hits the End Of the File (EOF.) That is what EOF stands for, End Of File. Inside the parentheses, is the number 1. This loop loops until the end of file #1. Do not use the pound (#) sign with this statement, it will only cause an error.
Inside of the Do Loop are the following statements:
Line Input #1, Addline
FinalText = FinalText + Addline
The first line reads, one line at a time out of file #1 and stores the received data in Addline. On the following line, Addline is appended to FinalText. The Loop then starts agian, first checking to see if it is at the end of the file #1. If it is, it exits the loop, otherwise it agian executes the body of the loop. It stores the next line of file #1 in Addline. Addline is again appended to FinalText, and so on. By the end of the file, FinalText will contain the entire file. Have fun, but don't clutter up your hard drive with too many files!
Copyright ©2000 by Kyle Baker. Please read our disclaimer.