Appending Text Question - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Appending Text Question

happymonkeyhappymonkey Posts: 1Member
I am writing a program that searches for a certain criteria in a text file. Once it finds it, it appends a timestamp to the end of the line.

I have it to where it will append to the end of the file but not the end of the line.

I could use some help please.

Thanks.

Comments

  • seancampbellseancampbell Pennsylvania, USAPosts: 684Member ✭✭✭
    I am not familiar with VB.Net's new version of file manipulation... However I do have a suggestion for you.

    Load the text file into memory 1 line at a time, after you analyze the line (and append the time stamp if needed) append the result to a String object that will contain the entire contents of the file...
    When you are done reading the file, close it, and write a new file over top of the old one... Here is a simple example (note this example does not have error handling):

    [This will search for the letter a in each line and append the date to the end of the line]
    [code]
    'This finds an available 'file number' to use
    Dim FF As Integer = FreeFile()
    Dim StrLine As String 'This will hold a single line of text
    Dim StrFile As String 'This will hold the new file contents
    'This opens the file for input (reading)
    FileOpen(FF, "C:Log.txt", OpenMode.Input)

    'Do While Not EndOfFile
    Do While Not EOF(FF)
    StrLine = LineInput(FF) 'When the line is read, it removes the carrage return
    If StrLine.IndexOf("a") <> -1 Then
    StrLine &= " " & Format(Now, "MMddyyyy")
    End If
    StrFile &= StrLine & vbNewLine 'vbNewLine puts the carrage return back on
    Loop

    FileClose(FF)
    'This opens the file for output (writing)
    FF = FreeFile()
    FileOpen(FF, "C:Log.txt", OpenMode.Output)
    PrintLine(FF, StrFile)
    FileClose(FF)
    [/code]
Sign In or Register to comment.