Results 1 to 6 of 6

Thread: Insert text at beginning of file

  1. #1

    Thread Starter
    Addicted Member Screamager's Avatar
    Join Date
    May 2001
    Location
    Dublin, Ireland
    Posts
    174

    Insert text at beginning of file

    Hello,

    Is it possible to add a line to the beginning of a textstream...or to read the textstream from .atendofstream and to the top? I need to write the contents to a textbox and need the newest entry (now appended at the end of the file) to be displayed first in the textbox...any ideas?

  2. #2
    Lively Member Pete Rosborough's Avatar
    Join Date
    May 2001
    Location
    Central NY
    Posts
    68
    Seems like it would be much easier with a database
    Real programs don't eat cache.

    Pete

  3. #3

    Thread Starter
    Addicted Member Screamager's Avatar
    Join Date
    May 2001
    Location
    Dublin, Ireland
    Posts
    174
    It's just a log file so i don't really want to use a database...

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

    <?>

    Code:
    Private Sub Command1_Click()
      Dim sFile As String, iNum As Integer
      Dim myVar As String
      sFile = "C:\my documents\myfile.txt"
      iNum = FreeFile
      Dim sArr()
      Dim i As Integer
      Open sFile For Input As iNum
      Do Until EOF(iNum)
        ReDim Preserve sArr(i)
        Line Input #iNum, myVar
        sArr(i) = myVar
        i = i + 1
      Loop
         Close iNum
    
    Open sFile For Output As iNum
      Print #iNum, "my new line"
      For i = 0 To UBound(sArr)
         Print #iNum, sArr(i)
       Next i
          Close iNum
    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

  5. #5
    Addicted Member jcouture100's Avatar
    Join Date
    Aug 1999
    Posts
    141
    Here is another option...

    Code:
    Private Sub BuildLogFile2()
    'This uses a Temp File to save log info
    'and then appends the temp file to the new log file.
        
        Dim strFile1 As String
        Dim strFile2 As String
        Dim strTemp As String
        Dim intNum1 As Integer
        Dim intNum2 As Integer
        
        
        strFile1 = "C:\My Documents\MyFile.log"
        strFile2 = "C:\My Documents\~MyFileLog.bak"
        
        'Do Clean up before processing
        'Use Resume Next in case file doesn't exists
        On Error Resume Next
        Kill strFile2
        
        'Now use Error Handler Routine
        On Error GoTo errHandler
        
        intNum1 = FreeFile
        
        'Back Up the Log File (Just to be safe)
        Name strFile1 As strFile2
        
        'Create New Log File
        Open strFile1 For Output As intNum1
        Print #intNum1, "New Line " & Now
        Close intNum1
        DoEvents
        
        'Append Old Log File to New Log File
        Open strFile1 For Append As intNum1
        intNum2 = FreeFile
        Open strFile2 For Input As intNum2
        Do Until EOF(intNum2)
            Line Input #intNum2, strTemp
            Print #intNum1, strTemp
        Loop
        Close intNum2
        Close intNum1
        
        'Clean Up
        Kill strFile2
        Exit Sub
        
    errHandler:
        'An Error occurred so set things back the way they were
        Close intNum2
        Close intNum1
        Kill strFile1
        Name strFile2 As strFile1
        MsgBox Err.Description, vbOKOnly, "Error " & Err.Number
    End Sub
    JC

  6. #6
    Megatron
    Guest
    Code:
    Open "Myfile" For input as #1
    tmp = input(lof(1),1)
    close #1
    
    Open "Myfile" for output as #1
    print #1, "MyStr"
    print #1, tmp
    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