Hi,
How do i read a text file line by line or to get vb to read a file up to the letter ';' and then continue. this is used for reading a line from a afile then doing a,b amd c with it then read the next line and do a, b, c with it.
Any ideas ?
Printable View
Hi,
How do i read a text file line by line or to get vb to read a file up to the letter ';' and then continue. this is used for reading a line from a afile then doing a,b amd c with it then read the next line and do a, b, c with it.
Any ideas ?
Take a look at the
Line Input#
statement...
Im not too sure of what your trying to do but this is
what i do for file input output
Private Sub cmdButton_Click()
Dim a,b as String
Open "DATA.TXT" For Input as #1 'or what path it is in
' ie... "C:\DATA.TXT"
Input #1, a , b
Debug.Print a , b
Close #1
End Sub
'Your text file should look like this
"Visual","Basic"
If You have say Dim num as single you can do stuff like
Input #1, num
picOutput.Print num * num
' as long as what your trying to read
is dimensioned with the right data type you should
be alright.
To format like a b c d e just do Debug.Print a;b;c;d;e
To format like a
b
c
d
e
just omit the ;'s Debug.Print a b c d e
You can also use Tab() to space your stuff
it is just like SetW() in C++ but for C++ you
have to add the header <iomanip> for manipulator
"A man ought to read as inclination leads him,
for what he reads as a task will do him little good"
Johnson.
What i am trying to achieve is to read a text file line by line so that i can get line one of the text file and if its equal to something then do blah, blah, THEN i want it to move onto the next line and do blah, blah with that line.
Code:'variable holding the lines
Dim Line As String
'open the file
Open "C:\Myfile.txt" For Input As #1
Do
'read a line
Line Input #1,Line
'do something with it
debug.Print Line
Loop Until EOF(1)=True
Close #1
It might not be a good idea to use Line as a variable name because it can be a little confusing.
What would it be if the variable Line would be strText then, coz i'm confused
Thanks for the code though
Form1,List1, C:\myfile.txt
On start mytext.txt should look like this
The line I am Looking for
Help me out
I know this works
The line I am Looking for
After running should look like this
My Fixed Line
Help me out
I know this works
My Fixed Line
The list box List1 is for display only
Code:'search for a particular line and do something with it
'read the file into an array and then back into your file
'and save it
Option Explicit
Option Compare Text
Private Sub Form_Load()
Dim myArr() As Variant
Dim myComp As String, myLine As String
Dim i As Integer, intNum As Integer
myComp = "The line I am Looking for"
intNum = FreeFile
'
'open file
Open "C:\myfile.txt" For Input As intNum
Do While Not EOF(intNum)
i = i + 1
ReDim Preserve myArr(1 To i) As Variant
Line Input #intNum, myLine
myArr(i) = myLine
myArr(i) = Trim(myArr(i))
If myArr(i) = myComp Then
'do whatever you want to here I change it to read "My Fixed Line"
myArr(i) = "My Fixed Line"
End If
Loop
Close #intNum
'open file
Open "c:\myfile.txt" For Output As intNum
For i = 1 To UBound(myArr)
Print #intNum, myArr(i)
List1.AddItem myArr(i)
Next
Close #intNum
End Sub
It's ok, i have sorted it out now, thanks all
Line is an actual VB method so you can use anything but Line. strLine, as you mentioned, is probably the best choice.Quote:
Originally posted by PsyVision
What would it be if the variable Line would be strText then, coz i'm confused
Thanks for the code though