CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2000
    Posts
    264

    Inserting a line in a text file

    I was wondering what the best way would be to do the following:

    I have a text file. The name of the text file is abc123.dat. What I would like to do, is open that file, and insert the name of the file (abc123 .. without the extension) as the first line of the file and then save the file. I know that I need to open the file, insert the data, and then save it but some code examples would be helpful. What should I use to insert the data into the file?

    Thanks in advance for any suggestions.
    Greg


  2. #2
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: Inserting a line in a text file

    Use this

    dim line as integer

    private sub InsertLine(byval strFile as string, byval intLineNum as integer, byval strLine as string)
    ' strFile is the path to the file you want to use
    ' strLine is the text you want to insert
    ' intLineNum is the number of the line where you want to insert the text

    dim ffileIn as integer
    dim ffileOut as integer
    dim strInput as string
    dim intLineCount as integer

    ffileIn = freefile
    open strfile for input as #ffileIn

    ffileOut = freefile
    open app.path & "\tmp.dat" for output as #ffileOut

    do while not(eof(ffileIn))

    lineCount = lineCount + 1
    line input #ffileIn, strInput

    if lineCount = intLinenum then
    print #ffileout, strLine
    end if

    print #ffileout, strInput

    loop

    close #ffilein
    close #ffileout

    filecopy app.path & "\tmp.dat", strFile

    end sub



    This should do the trick

    Tom Cannaerts
    [email protected]

    The best way to escape a problem, is to solve it.
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  3. #3
    Join Date
    Jan 2000
    Posts
    264

    Re: Inserting a line in a text file

    Absolutely the perfect code...except...instead of being a Private Sub, you might want to try Private Function!! :-)




  4. #4
    Join Date
    Feb 2000
    Location
    garden grove, california
    Posts
    64

    Re: Inserting a line in a text file

    I made a text program like notepad, you can download the project files and exe in a file from at:
    http:\\gamerzpro.virtualave.net\TextWriter.zip


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

Featured