chitika

Wednesday, May 25, 2011

vb: how to read file

The following lists three ways of reading file. They are read file line by line, read whole file, read file letter by letter (character by character), and read comma-delimited file
1. How to read file line by line?

Open App.Path & "\temp.txt" For Input As #1

Do While Not EOF(1)
Line Input #1, wholeLine
wholeLineAll = wholeLineAll & wholeLine & vbCrLf
DoEvents
Loop

Close #1

2. How to read whole file?
Open App.Path & "\temp.txt" For Input As #1

myString = Input(FileLen(App.Path & "\temp.txt"), #1)

Close #1

3. How to read file letter by letter?
Open App.Path & "\temp.txt" For Input As #1

Do While Not EOF(1)
MyChar = Input(1, #1) 'one char a line
If (MyChar = "#") Then
count_number = count_number + 1
End If
Loop

Close #1

4. How to read comma-delimited file?
Private Sub Command1_Click()

Dim strIn As String
Dim fields() As String
Dim nFields As Integer
Dim i As Integer

Open "C:\test.csv" For Input As #1
While Not EOF(1)
Line Input #1, strIn
fields = Split(strIn, ",")
nFields = UBound(fields)
For i = 0 To nFields
Debug.Print fields(i)
Next i
Wend

Close #1

End Sub

No comments:

Post a Comment

For other hundreds of computer repair tips, tricks, tweaks, guide, help, please go to


http://website.edusoftmax.com