How to write to a text file
Using VB.NET there are many ways to write and read from a textfile.
I am going to show you how to do it a certain way;
1. Open a text file.
2. Open up a second text file.
3. Read all the contents from the first file.
4. Start writing to the second file with the first text file data and add any extra data.
5. Destroy the first file.
6. Rename the second file in the first files name.
7. Close the file.
Here goes
Open up a new VB application AND rename it TextFileProgram1 and add;
a textbox and rename it txtInput
a button and rename it cmdInput The number we set to refer to the
file.
Section 1:
Within the command button sub routine write: If it is .input then read from that file
If it is .output then writes to that file
FileOpen(1, "test.txt", OpenMode.Input)
FileOpen(2, "test.tmp", OpenMode.Output)
We use the FileOpen() function to open a text file. The name of the text file.
Code update: So it should look like this;
Public Class Form1
Private Sub cmdInput_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles cmdInput.Click
FileOpen(1, "test.txt", OpenMode.Input)
FileOpen(2, "test.tmp", OpenMode.Output)
End Sub
End Class
The above code has opened 2 files.
Section 2:
Next we need to read from the first text file;
Do While Not EOF(1)
dataFromFirstFile = LineInput(1)
PrintLine(2, dataFromFirstFile)
Loop
We use a Do While Loop to read from the first file into a variable (dataFromFirstFile) and then
writes to the second file.
Lets take one line at a time;
Do While Not EOF(1) - EOF means End Of File and the (1) refers to test.txt. So continue
the do while loop whilst it is not the end of the file!
dataFromFirstFile = LineInput(1) – Read each line from file 1 and then add to a variable
named dataFromFirstFile. DO NOT FORGET TO DECLARE THE VARIABLE AS AN
OBJECT!
PrintLine(2, dataFromFirstFile) – write to the second file with the content of the
dataFromFirstFile variable.
Loop – I am not explaining this, you should know this already!
Code update: So it should look like this;
Public Class Form1
Private Sub cmdInput_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles cmdInput.Click
Dim dataFromFirstFile As Object
FileOpen(1, "test.txt", OpenMode.Input)
FileOpen(2, "test.tmp", OpenMode.Output)
Do While Not EOF(1)
dataFromFirstFile = LineInput(1)
PrintLine(2, dataFromFirstFile)
Loop Object variable allows a value of any
End Sub
End Class type!
Section 3:
We now want to add data to the end of our text file. We do this by firstly getting information from
the user. We can do this by an inputbox but in this case we are using a textbox.
newData = TxtInput.Text
and
PrintLine(2, newData)
The PrintLine() function writes to the second file with the value from a textbox.
Code update: So it should look like this;
Public Class Form1
Private Sub cmdInput_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles cmdInput.Click
Dim dataFromFirstFile As Object
Dim newData As String
FileOpen(1, "test.txt", OpenMode.Input)
FileOpen(2, "test.tmp", OpenMode.Output)
Do While Not EOF(1)
dataFromFirstFile = LineInput(1)
PrintLine(2, dataFromFirstFile)
Loop
newData = txtInput.Text
PrintLine(2, newData)
End Sub
End Class
All there is to do is close the files, destroy and rename.
FileClose(1)
FileClose(2)
Kill("test.txt") 'Deletes the Original File
Rename("test.tmp", "test.txt")
Code update: Lets look at the final code;
Public Class Form1
Private Sub cmdInput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdInput.Click
Dim dataFromFirstFile As Object
Dim newData As String
FileOpen(1, "test.txt", OpenMode.Input) ‘Opens file 1 for reading
FileOpen(2, "test.tmp", OpenMode.Output) ‘Opens file 2 for writing
Do While Not EOF(1) ‘Loop while the file is not at the end
dataFromFirstFile = LineInput(1) ‘Reads file 1 and stores EACH line into the variable
PrintLine(2, dataFromFirstFile) ‘Writes to file 2
Loop
newData = txtInput.Text ‘User input and then stores in a variable
PrintLine(2, newData) ‘Writes to file 2 with the new user input
FileClose(1) ‘Close both files
FileClose(2)
Kill("test.txt") 'Deletes the Original File
Rename("test.tmp", "test.txt") ‘Renames the file 2 to the file 1
End Sub
End Class
Before you try this you need to put a text file in the correct directory or it will not work;
TextFileProgram1\TextFileProgram1\bin\Debug
One way to get around the problem of adding a text file yourself is to add an IF statement to see if the file exists;
This statement refers to an already existing ‘library’ of
code that allows us to use the File.Exists function.
Alot more on this next year in A2.
Imports System.IO
Public Class Form1
Private Sub cmdInput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdInput.Click
Dim dataFromFirstFile As Object
Dim newData As String
IF statement to see whether text file exists!
newData = txtInput.Text
If File.Exists("test.txt") = True Then
FileOpen(1, "test.txt", OpenMode.Input)
FileOpen(2, "test.tmp", OpenMode.Output)
Do While Not EOF(1)
dataFromFirstFile = LineInput(1)
PrintLine(2, dataFromFirstFile)
Loop
I have added 2 message boxes to tell the user that the
newData = txtInput.Text data has been written to the new file!
PrintLine(2, newData)
MsgBox("Text written to file")
FileClose(1)
FileClose(2)
Kill("test.txt") 'Deletes the Original File
Rename("test.tmp", "test.txt") The ELSE bit of the IF statement that opens a text file and
Else then writes the user data to the new text file and them
FileOpen(2, "test.txt", OpenMode.Output) closes it.
PrintLine(2, newData)
MsgBox("Text File created and data written to file") Please Note that I have now moved the newData =
FileClose(2) txtInput.Text to the top outside of the IF statement.
End If
End Sub
End Class