Results 1 to 10 of 10

Thread: Text Files

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    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 ?

  2. #2
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    Take a look at the

    Line Input#

    statement...

    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    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.

  5. #5
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715
    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

  6. #6
    Guest
    It might not be a good idea to use Line as a variable name because it can be a little confusing.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    What would it be if the variable Line would be strText then, coz i'm confused

    Thanks for the code though

  8. #8
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    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

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    It's ok, i have sorted it out now, thanks all

  10. #10
    Guest
    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
  •  



Click Here to Expand Forum to Full Width