VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



File Handling Examples in VB

by Abhijit Desai (3 Submissions)
Category: Files/File Controls/Input/Output
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Sun 23rd June 2002
Date Added: Mon 8th February 2021
Rating: (1 Votes)

File Handling Examples in VB

Rate File Handling Examples in VB



'---------------------------------------
'To get available file number
Dim Filenum As Integer
Filenum = FreeFile

'Now you can use this filenumber to open a file in any mode
'The following code will use the same variable filenum in all examples

'example1 To open a file "c:\ex1.txt" in sequential mode and read contents in a multiline textbox 'named text1
Open "c:\ex1.txt" For Input As Filenum
Text1.Text = Input(LOF(Filenum), Filenum)
'close the file
Close Filenum

'example2 To open a file "c:\ex2.txt" in sequential mode and write contents of a multiline 'textbox named text1 to the file
Open "c:\ex2.txt" For Output As Filenum
Print #Filenum, Text1.Text
Close Filenum

'example3 To open a file "c:\ex3.txt" in sequential mode and write values of multiple variables 'to the file
Open "c:\ex3.txt" For Output As Filenum
intVar = 10
dblVar = 100.11
strVar = "ABCD"
Write #Filenum, intVar, dblVar, strVar
Close Filenum
'Note: You can separate individual variables by comma or semicolon, but they all will be written 'into file separated by commas. String varibles will be enclosed in doublequotes, Subsequent 'write statements will write the values on newlines.

'example4 To open a file "c:\ex3.txt" in sequential mode and read contents of file into multiple 'variables
Open "c:\ex3.txt" For Input As Filenum
Dim intVar As Integer, dblVar As Double, strVar As String
Input #Filenum, intVar, dblVar, strVar
Close Filenum

'example5 To open a file "c:\ex5.bsf" in random access mode and write contents of a user defined 'type named mystruct to the file. we will consider that type mystruct has been defined already 'and a varible myVar of type mystruct has been defined as follows
Private Type myStruct
  intVar As Integer
  dblVar As Double
  strVar As String * 20
End Type
Dim myVar As myStruct
Dim recNum As Integer ' variable to hold value for recordnumber

Open "c:\ex5.bsf" For Random As Filenum Len = Len(myVar)
myVar.intVar = 100
myVar.dblVar = 1000.11
myVar.strVar = "Abhijit"
recNum = 1
'Now use myvar to write above values to the file
Put #Filenum, recNum, myVar
Close Filenum

'example6 To open a file "c:\ex5.bsf" in random access mode and read contents of the file (first 'record) into above user defined type mystruct. Data in the file can be collected from member 'variables of  type myvar.
Open "c:\ex5.bsf" For Random As Filenum Len = Len(myVar)
Get #Filenum, recNum, myVar
'Print the data on the form
Print myVar.intVar
Print myVar.dblVar
Print myVar.strVar
Close Filenum

'example7 To open a file "c:\ex7.txt" in binary mode and write contents of multiple variables
'into file
Open "C:\ex7.dat" For Binary As Filenum
intVar = 100
strVar = "MyAbhijit"
byteLocation = 1
'Now put contents of intvar into file starting from byte location 1
Put #Filenum, byteLocation, intVar
'If value for bytelocation is not supplied following put statement will write the value of 'supplied variable into file from next avilable bytelocation i.e. 3
Put #Filenum, , strVar
Close Filenum

'example8 To open a file "c:\ex7.txt" in binary mode and read contents of file into multiple 'variables
Open "C:\ex7.dat" For Binary As Filenum
Dim strVar As String * 7
Dim intVar As Integer
byteLocation = 1
'Now read contents of file into intvar starting from byte location 1
Get #Filenum, byteLocation, intVar
'If value for bytelocation is not supplied following put statement will read the value from file 'from next avilable bytelocation i.e. 3, into supplied variable
Get #Filenum, , strVar
Close Filenum

'In all above examples while opening file if you want to use file number directly instead of 'filenum use it with prefix #
'e.g. open "C:\Ex1.txt" for input as #1


Download this snippet    Add to My Saved Code

File Handling Examples in VB Comments

No comments have been posted about File Handling Examples in VB. Why not be the first to post a comment about File Handling Examples in VB.

Post your comment

Subject:
Message:
0/1000 characters