Jump to content

Recommended Posts

Posted

Hi guys,

How can i write a line at the end of a textfile?

I have this sofar: but it aint working:

$file = FileOpen("test.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

; Read in 1 character at a time until the EOF is reached
While 1
    $chars = FileRead($file, 1)
    If @error = -1 Then go()
Wend



func go()
FileWriteLine($file, "This is the EOF")
FileClose($file)
exit
endfunc
Posted

I dont know how else to do it...

its like this:

here is the text file

test.txt

line 1
line 2
line 3
bla
bla
sdfsdf
sdfsd
fs
df
dsf

this textfile is not always the same lenght.... and at the bottom of the textfile... i want to write a line of text.... howto do that?

Posted (edited)

By just close the open file for read, and then write to the file directly.

$file = FileOpen("test.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

; Read in 1 character at a time until the EOF is reached
While 1
    $chars = FileRead($file, 1)
    If @error = -1 Then ExitLoop
Wend
FileClose($file)

FileWriteLine("test.txt", "This is the EOF")
Reading 1 char at a time, would be slow? But if that is what you want.

Else you could use

FileRead($file, FileGetSize("test.txt"))

Read the whole file, and add your last line, then write it all at once.

Edited by MHz
Posted

  MHz said:

Read the whole file, and add your last line, then write it all at once.

<{POST_SNAPBACK}>

Or just open the file in append mode, and then write your line. Much easier/faster.

:(

  • 2 years later...
Posted

  blindwig said:

Or just open the file in append mode, and then write your line. Much easier/faster.

:)

I understand this part how do you append to the top of a text file? I am making a log file and I want the newest log entries at the top of the file so I don't have to scroll down.

Posted

  Senton-Bomb said:

Look in the helpfile for

_FileReadToArray

_ArrayAdd

_FileWriteFromArray

went with this since my logs are small but seems to work good.

#include <file.au3>
$file = FileOpen('c:\error.log', 0)
    
$LogStore = FileRead($file)
FileClose($file)

$file = FileOpen('c:\error.log', 2)
FileClose($file)

$file = FileOpen('c:\error.log', 1)

FileWrite($file, "NEW STUFF HERE"  & @CRLF)

;~ OLD STUFF BACK IN END OF FILE
FileWrite($file, $LogStore & @CRLF)

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...