Jump to content

FileWrite


Morelli

Recommended Posts

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

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 by Guest
Link to comment
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

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
  • 2 weeks later...

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...