|
-
Aug 22nd, 2000, 08:01 AM
#1
Thread Starter
Frenzied Member
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 ?
-
Aug 22nd, 2000, 08:05 AM
#2
Frenzied Member
Take a look at the
Line Input#
statement...
'Buzby'
Visual Basic Developer
"I'm moving to Theory. Everything works there."
-
Aug 22nd, 2000, 11:01 AM
#3
Dazed Member
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.
-
Aug 22nd, 2000, 02:59 PM
#4
Thread Starter
Frenzied Member
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.
-
Aug 22nd, 2000, 03:02 PM
#5
Fanatic Member
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
-
Aug 22nd, 2000, 03:17 PM
#6
It might not be a good idea to use Line as a variable name because it can be a little confusing.
-
Aug 22nd, 2000, 04:01 PM
#7
Thread Starter
Frenzied Member
What would it be if the variable Line would be strText then, coz i'm confused
Thanks for the code though
-
Aug 22nd, 2000, 04:08 PM
#8
_______
<?>
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
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
-
Aug 22nd, 2000, 04:16 PM
#9
Thread Starter
Frenzied Member
It's ok, i have sorted it out now, thanks all
-
Aug 22nd, 2000, 05:10 PM
#10
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
Line is an actual VB method so you can use anything but Line. strLine, as you mentioned, is probably the best choice.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|