Reading and writing to files

Reading and writing to files

When you read and write files from Visual Basic, you need to follow these steps:
1) Get a free file number using the FreeFile function. This is used to identify the file once you have opened the file.
2) Use the Open function to open the file for Input (read) or Output (write).
3) Perform an action on the file, either reading it or writing to it.
4) Close the file

To get a free file number, you use the FreeFile function:

‘ Get a free file number
nFileNum = FreeFile

Then, to open the file, we use the Open function. This takes the following syntax:

Open Filename For Method As FileNumber 

Filename is the name of the file, Method is Input or Output and FileNumber is a free file number. For example, the following code opens test.txt for Input:

‘ Get a free file number
nFileNum = FreeFile
‘ Open Test.txt for input. App.Path returns the path your app is saved in
Open App.Path & “\test.txt” For Input As nFileNum

To get some text from the file, we use the Input statement:

Input(Length, FileNumber)

So, if we wanted to load the first 100 characters from the file, we would use

Input(100, nFileNum)

where nFileNum is the file number we opened the file with. You can then call the function again to read the next 100 characters and so on. If you want to move where you currently are in the file, use the Seek function:

Seek(FileNumber, Position)

For example, the following code loads the characters from 0 to 10, from 10 to 20, and then from 30 to 40:

sText = Input(10, nFileNum) ‘first 10 chars
sText = sText & Input(10, nFileNum) ‘next 10 chars
Seek nFileNum, 30 ‘go to char 30
sText = sText & Input(10, nFileNum) ‘chars 30-40

If you want to get the whole file, you can use the LOF (Length of File) function. The following line returns the whole file:

Input(LOF(nFileNum), nFileNum)

If, on the other hand you want to write to the file, you use the Write function:

Write #nFileNum, Text

where, as usual nFileNum is the file number we opened the file with, and Text is the text we want to write to it. Please note that any text you write to the file will overwrite any existing text. Also note that each time you call the Write function, VB will automattically add a new line. So, if you write:

Write #nFileNum, “hello”
Write #nFileNum, “hello again”

you will get

hello
hello again

instead of

hellohello again

When you have finished with the file, you use the Close statement to close the file. VB does actually do this for you when your application closes, but it is a good habit to get into.

Close(nFileNum)

To put this all together, take a look at the following code. This reads the whole contents of Test.txt into TextBox1.

Dim nFileNum As Integer

‘ Get a free file number
nFileNum = FreeFile

‘ Open Test.txt for input. App.Path returns the path your app is saved in
Open App.Path & “\Test.txt” For Input As nFileNum

‘ Read the contents of the file into TextBox1
TextBox1.Text = Input(LOF(nFilenum), nFileNum)

‘ Close the file
Close nFileNum

and, to write the contents of TextBox1 back to the file, you use the following code:

Dim nFileNum As Integer

‘ Get a free file number
nFileNum = FreeFile

‘ Create Test.txt
Open App.Path & “\test.txt” For Output As nFileNum

‘ Write the contents of TextBox1 to Test.txt
Write #nFileNum, TextBox1.Text

‘ Close the file
Close nFileNum
 

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.