Results 1 to 2 of 2

Thread: Replace a line in a text file

  1. #1

    Thread Starter
    Hyperactive Member rockies1's Avatar
    Join Date
    Jul 1999
    Location
    Stuck at work
    Posts
    375

    Talking

    I have a text file that I need to replace the 3rd line of data in..

    What is the best way to do this?

    For example:

    I have c:\testfile.txt

    It reads:

    line 1
    line 2
    line 3
    line 4

    I want to change it to read:

    line 1
    line 2
    New line 3
    line 4

    How can I do that?

    Thanks!
    Morgan
    [email protected] - Home
    [email protected] - Work
    Using VB6 SP6 but trying to learn VB2005EE

  2. #2
    Guest
    Code:
    Dim iCount As Integer
    Dim TmpFile As String
    
    Open "myfile.txt" For Input As #1
        Do Until EOF(1)
            Line Input #1, tmp
            iCount = iCount + 1
            If iCount = 3 Then tmp = "NewLineToBePrinted"
            TmpFile = TmpFile & tmp & vbCrLf
        Loop
    Close #1
    
    Open "myfile.txt" For Output As #1
    Print #1, TmpFile
    Close #1

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