How do I open a file for input, output or to append, I know you use
open [filename] for [input/output/append] as #[#]
but how do you write to a file and read from a file?
please help me....
Printable View
How do I open a file for input, output or to append, I know you use
open [filename] for [input/output/append] as #[#]
but how do you write to a file and read from a file?
please help me....
Code:'writing or reading data to or from a text file
'open for append adds the data to the end of file
'open for input opens the file for read only
'open for output opens the file and overwrites the file
'
'
Private Sub Command1_Click()
'this is the number of your file
Dim intFilenum As Integer
'set it to the next available free number
intFilenum = FreeFile
'this is your filename
Dim strFile As String
strFile = "A:\myfile.txt"
'this is the text you want to save (textbox)
Dim strText As String
strText = Text1.Text
'open the file for writing to as next freefile number
Open strFile For Append As intFilenum 'see above options
'write to the file
Write #intFilenum, strText 'to read use [input #filenum,strtext]
'close the file
Close intFilenum
End Sub
To Write to a file:
To Read a file:Code:Open "MyFile.txt" For Output As #1
Print #1, "Hello"
Close #1
Code:Open "MyFile.txt" For Input As #1
Input #1, MyVar
Close #1
'Display the result
MsgBox (MyVar)
There are many different ways.
Input opens a file strictly for input.
Output opens a file, and overwrites whatevre is there.
Append opens a afile and places new data on the end of the file.
There is also the Get / Put statemnets that are used on Binary / Random file modes.Code:'INPUT METHODS.
'inpute a Line
Line Input #1, strLine
'inputs a set amount of characters. In this cse the whole file
strFile = Input(lof(fileNum), #fileNum)
'inputs a number of variables from a comma delimted file
Input #1, var1, var2, var3
'OUTPUT METHODS.
'Prints text as is.
Print #1, strText
'Inserts commas between variables, and places " around strings.
Write #1, Var1, Var2