Results 1 to 11 of 11

Thread: Replacing Lines in an text file

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2000
    Posts
    316
    I need to replace the first line of a text file with some other text. Can anyone give me an example of how to do it? This is what I have so far, but it just appends to the bottom of the file.

    Code:
    Private Sub cmdConvert_Click()
        
        Dim intnum As Integer
        intnum = FreeFile
            Open CommonDialog1.FileName For Append As intnum
              Print #intnum, "This is the line I want to replace"
           Close #intnum
    End Sub
    Using VS 6 Enterprise w/ SP5 & Windows 2000 Professional

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

    <?>

    Code:
    Option Explicit
    'read the file into an array and change the 1st line inside the
    'array and then read the array back into the file
    
    Private Sub Command1_Click()
        Dim myFile As String, intNum As Integer, iCount As Integer
        Dim myString As String
        
        myFile = "C:\my documents\myfile.txt"
        intNum = FreeFile
        Dim myVar()
        
        Open myFile For Input As intNum
        
          Do While Not EOF(intNum)
             Line Input #intNum, myString
             ReDim Preserve myVar(iCount)
             
          If iCount = 0 Then
            myVar(iCount) = "The line I want to insert into the file"
          Else
            myVar(iCount) = myString
          End If
            iCount = iCount + 1
                Loop
          Close #intNum
        
        Open myFile For Output As intNum
          For iCount = LBound(myVar) To UBound(myVar)
             Print #intNum, myVar(iCount)
          Next iCount
          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

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Code:
    Dim buffer as string, lines() as string
    'open the file binary
    Open file for binary as 1
       buffer=space(lof(1))
       get#1,,buffer
       'split up the lines
       lines=split(buffer,vbcrlf)
       'modify the first line
       lines(0)="The line I want to insert into the file"
       'join the lines and put it back in the file
       put#1,1,join(lines,vbcrlf)
    close 1
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

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

    <?>

    ouch!...

    This learning is tough business!

    Too cool.
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  5. #5
    Addicted Member
    Join Date
    Sep 2000
    Posts
    138
    Very cool indeed, except for a small thing -- both codes leave out a vbCrLf. For Joe's code, the whole file becomes a single line. For kedaman's file, the first and second lines merge.

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

    <?>

    How do you come to that conclusion. I input each line into and array and then I replace a line and I output by line so my file still has all the lines. If you doubt that just open the file afterward with command2 and do a line input..you will get the lines not just one line. If what you say is true when you lineinput you will get the complete file.
    Code:
    Private Sub Command2_Click()
    Dim myVar As String
    
    Open "C:\my documents\myfile.txt" For Input As #1
    Do While Not EOF(1)
      Line Input #1, myVar
      List1.AddItem myVar
      Loop
      
    End Sub
    comment out the loop and you will see that all you get
    it the first line of the file..use the loop and you will get
    individual lines.



    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  7. #7
    Addicted Member
    Join Date
    Sep 2000
    Posts
    138
    I am terribly sorry for having made a wrong comment on kedaman's code. His code is perfect. In fact kedaman's join(lines,vbCrLf) has made up for the loss of each CrLf in the use of the Split function, including the missing of the CrLf at the end of the replacing first line. But I'm afraid, Joe, you are perhaps wrong on that point. When Line Input is used to read a text file, it reads a line at a time and stops at the CrLf. That is to say, it does not read the CrLf. So no lines in the array end in a CrLf. When the array is printed back into a file, the CrLfs which were included in the original file are now all lost.

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

    <?>

    If you run my code from above and then run this code
    which opens the file I masacrated above, reads it, and then splits it on vbCrlf I don't think I am wrong.

    Code:
    Private Sub Command2_Click()
     
     Dim sHolder As String
     Dim intNum As Integer
     Dim sFileName As String
        
        'open for binary and read
        sFileName = "C:\My Documents\MyFile.txt"
        intNum = FreeFile
        
        Open sFileName For Binary As intNum
          sHolder = Space(LOF(1))
          Get #1, , sHolder
        Close intNum
        
        
     'you are wrong as I split the file on vbCrlf
    'if it doesn't exits, how can the split work
    
        Dim myVar As Variant
        myVar = Split(sHolder, vbCrLf)
        Dim i
        For i = LBound(myVar) To UBound(myVar)
        List1.AddItem myVar(i)
        Next i
        
    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
    Addicted Member
    Join Date
    Sep 2000
    Posts
    138
    Quotes:
    --------------------------------------
    ¡°Why HeSaidJoe? Why not IHailJoe?¡±, said xmin when he received the first reply to his first post on this and ever Forum on September 26, 2000.
    --------------------------------------

    Thank you very much, HeSaidJoe. You are right. Your codes help me finally seem to understand what has happened to the Open statement. As a matter of fact I had tried the Line Input statement before I put up the last post. But I didn¡¯t test reading the rewritten file with the Open statement. I tested it by double clicking the file. When I later experimented with your List1.AddItem code, it seemed that the original separators had been replaced by tabs (of fourteen spaces on my Notepad). That is perhaps due to the Print statement (while the Write statement uses quotes as separators). Of course, so long as the lines are separated by something and Line Input works ok and I have learnt something, my goal has reached.

    Thank you again.

    --------------------------------------
    IHailJoe

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

    <?>

    xmin

    Wouldn't you know...
    He Said Joe is a line from a song and since Wayne was gone and my 1st name is Joseph {which I wouldn't use: no offence to anyone named Joseph}, I grabbed on to HeSaidJoe.

    PS. You are welcome...kedaman's code blows mine out of the water on this one so I would disect his code and store it in the old memory banks.

    later
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  11. #11
    Addicted Member
    Join Date
    Sep 2000
    Posts
    138
    Thank you again for your information, HeSaidJoe. I really didn¡¯t know that until you told me. Do you think mine was a silly ignorance?

    When I said ¡°Why ¡*? Why not ¡*?¡± for the first time I was making a well meant delighted joke on your name (HeSaidJoe) (Perhaps I shouldn't have done that?) and, at the same time, expressing my joy and gratefulness to you for your timely and quick help to me. You don¡¯t know how I was desperate and how I nearly went mad at the problem I was facing in using VB to design my Computer and Information Technology Examination System. I was caught in the trap for a month or so and I found no solution. I knew nothing about any VB forums at the time, but I thought of the Internet. I logged in to Yahoo and typed in ¡°VB Forum¡± and found this forum. I put up a post and got a first reply, your reply, soon afterwards. ¡°I didn¡¯t expect any reply to come so soon¡±, as many new members of us often say. I was surprised and excited and glad. I nearly cried out to hail for your help and my good luck (IHailJoe). And also I found your name interesting. So I made that joke.

    With your help I finally solved the problem and my System is getting on well now to be waiting for finishing touches. I appreciate your help very much, and any help from other members of our forums too. I also greatly appreciate our forums. I am grateful to the hosts/hostesses of the forums for having provided such a good place for us to learn, to play, to make friends, and to share what we have had and what, perhaps, we will have.

    Then, finally, when I sometimes mention again the ¡°Why ¡*? Why not ¡*?¡± or any relations to that I simply remind me of your help. And I hope I would be of some help to others.

    Thank you for your attention.

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