Results 1 to 7 of 7

Thread: (VB6) How to append text before first line in a text file ?

  1. #1

    Thread Starter
    Lively Member amolt's Avatar
    Join Date
    Aug 2006
    Location
    INDIA
    Posts
    80

    Question (VB6) How to append text before first line in a text file ?

    Hi all,

    I need to append a text file using generic file I/O commands in VB6.
    How do I append my text to first position in text file?

    I tried working like :

    Code:
        
        Dim FileNo As Integer
        Dim FilePath As String
        
        FileNo = FreeFile
        FilePath = "C:\Samplefile.txt"
    
        Open FilePath For Binary As FileNo
        Seek FileNo, 1
        Put FileNo, , "First Text" & vbCrLf
        Put FileNo, , "Second Text" & vbCrLf
        Close FileNo
    but this overwrite the first line in my text file.

    Regds,

  2. #2
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Re: (VB6) How to append text before first line in a text file ?

    something like this...
    Code:
    Private Sub Command1_Click()
        Dim FilePath As String
        Dim StrVal As String
        
        FilePath = "C:\Samplefile.txt"
        
        Open FilePath For Input As #1
            StrVal = Input(LOF(1), #1)
        Close #1
        
        Open FilePath For Output As #1
        Print #1, "First Text"
        Print #1, "second Text"
        Print #1, StrVal
        Close #1
    End Sub
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  3. #3

    Thread Starter
    Lively Member amolt's Avatar
    Join Date
    Aug 2006
    Location
    INDIA
    Posts
    80

    Re: (VB6) How to append text before first line in a text file ?

    Hey hi seenu,

    thanks for the reply,

    one thing is in my mind don't you think there isn't any way besides reading the whole file and writing it back ?

    Regds,

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: (VB6) How to append text before first line in a text file ?

    The fastest way is to read the text file into an array, open a new text file, write your text to the first line and then append the array to the text file...

    here is the code to read the file into the array in 1 go....
    Code:
    Dim FilePath As String
        
    FilePath = "C:\Samplefile.txt"
    
    '~~> Open file and read it into an array
    Open FilePath For Binary As #1
    MyData = Space$(LOF(1))
    Get #1, , MyData
    Close #1
    Last edited by Siddharth Rout; Oct 30th, 2009 at 04:28 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  5. #5

    Thread Starter
    Lively Member amolt's Avatar
    Join Date
    Aug 2006
    Location
    INDIA
    Posts
    80

    Re: (VB6) How to append text before first line in a text file ?

    Hi koolsid,

    Well I need to know how to write a text to the first postion in a previous written file without reading the whole file and write it back.

    Regds,

  6. #6
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: (VB6) How to append text before first line in a text file ?

    Appending to a location that is not the end of the file will require to read the whole file in, update it, and then write it back to file.

    What you can do is read file into two arrays, insert the new data into the begining of the array followed by rest of the text and update it back to the file...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  7. #7

    Thread Starter
    Lively Member amolt's Avatar
    Join Date
    Aug 2006
    Location
    INDIA
    Posts
    80

    Re: (VB6) How to append text before first line in a text file ?

    Hi KoolSid,

    Well I guess ,, there isn't any way to just append some text on the first position in a file too.
    Well I worked as like this too.

    Thanks

    Regds,

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