Jump to content

File writing help?


Recommended Posts

Hello, I'm trying to store some positions in a .txt file, but whenever I use the command to save the position, it writes over the same line and replaces the first position saved. Here's my code

function saveCoords(source) 
local x,y,z = getElementPosition( source ) 
local stringPos = string.format("%f, %f, %f \n ",x,y,z) 
local hFile = fileOpen("coords.txt",false)  
fileWrite(hFile,stringPos) 
fileClose(hFile) 
end 

I've tried the code without closing the file, same thing happens, I've also tried the code without the \n, and I get the same result. (I'm really sorry if you don't understand some of the things that I wrote, English is not my first language)

Link to comment
  • Moderators
function saveCoords(source) 
    local x,y,z = getElementPosition( source ) 
    local stringPos = string.format("%f, %f, %f \n ",x,y,z) 
    local hFile = fileOpen("coords.txt",false) 
fileSetPos(hFile,fileGetSize ( hFile )) -- I added this. 
    fileWrite(hFile,stringPos) 
    fileClose(hFile) 
  
    end 

You can get the filesize, which is the amount of bytes/characters in the document.

When you set the read position to the size of the document. It will start writing at the bytes after that.

Good luck!

Link to comment

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