MarkMarkMark Posted June 16, 2005 Posted June 16, 2005 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
MHz Posted June 16, 2005 Posted June 16, 2005 FileOpen() has the file open for read, but you write to it, at the last?
MarkMarkMark Posted June 16, 2005 Author Posted June 16, 2005 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?
MHz Posted June 16, 2005 Posted June 16, 2005 (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 June 16, 2005 by MHz
blindwig Posted June 16, 2005 Posted June 16, 2005 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. My UDF Threads:Pseudo-Hash: Binary Trees, Flat TablesFiles: Filter by Attribute, Tree List, Recursive Find, Recursive Folders Size, exported to XMLArrays: Nested, Pull Common Elements, Display 2dSystem: Expand Environment Strings, List Drives, List USB DrivesMisc: Multi-Layer Progress Bars, Binary FlagsStrings: Find Char(s) in String, Find String in SetOther UDF Threads I Participated:Base64 Conversions
mbkowns Posted February 7, 2008 Posted February 7, 2008 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.
NELyon Posted February 7, 2008 Posted February 7, 2008 Look in the helpfile for _FileReadToArray _ArrayAdd _FileWriteFromArray
mbkowns Posted February 7, 2008 Posted February 7, 2008 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)
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now