Morelli Posted March 26, 2008 Share Posted March 26, 2008 I've been recently exploring the FileWrite functions and I've noticed that when a line is written, it overwrites the first line in the file (it doesn't append the line). Here's the code I've been testing on: function writeonFile( sourcePlayer, command ) local hFile = fileOpen("TEST.txt" ) if hFile then local check = fileWrite(hFile, "This is a test file 2! \n") if ( check ) then outputChatBox( "You wrote something down" ) else outputChatBox( "Didn't work...." ) end fileClose(hFile) else outputConsole("Unable to open test.txt") end end addCommandHandler( "w", writeonFile) Another person has reported the same problem, here. Quote from my TEST.txt, even though the command has been used multiple times, there is only one line of text. This is a test file! Any suggestions/ideas on how to make it append the entry? Link to comment
Stevvo Posted March 26, 2008 Share Posted March 26, 2008 (edited) What up Morelli. Use fileRead ( theFile, bytes ) where bytes is the number of bytes forward you want to go through in the file, and then just discard the data fileRead returns. Or use sql db or xml to store data in files, much more practical. Edited March 26, 2008 by Guest Link to comment
Morelli Posted March 26, 2008 Author Share Posted March 26, 2008 What up Morelli,Don't use the file functions, use the xml functions for storing stuf in files, makes it easyer. As for your question, i don't know the anser. Easier or not, I'm looking for potential bugs. I tried writing the file in the native lua way of: io.write("text", "a") etc. However it showed up as undefined global variable 'io'. Link to comment
Jumba' Posted March 26, 2008 Share Posted March 26, 2008 |Eh, I ddont know if this may be it, be I used \n\r instead of just \n maybe that'd work? Link to comment
lil Toady Posted March 26, 2008 Share Posted March 26, 2008 as wiki says for fileWrite: "Writes one or more strings to a the given file, starting at the current read/write position." I suppose when you open the file the position is set at the start of the file, so you'll have to read the it, and fileSetPos to add a line instead of rewriting it Link to comment
CodeMaster Posted April 6, 2008 Share Posted April 6, 2008 As you can see in my thread, I gave up from FileWrite, and passed to XML. XML is more conventional and easier way to store any data Cheers Nidza Link to comment
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