JAVAAA Posted May 12, 2017 Share Posted May 12, 2017 (edited) Hey, I am asking how to write a new string before the original string that is existed in the file. Example: txt file contains "100" then I want to write a string before "100" My Code: function addUpdate(content) local file = "UpdatesLog.txt" local openFile = fileOpen(file) if not openFile then outputChatBox("Error loading file") return false end local date, time = getTimeDate() fileSetPos(openFile, fileGetPos(openFile)) if (fileGetSize(openFile) == 0) then fileWrite(openFile, "["..date.."]", content) else fileSetPos(openFile, fileGetSize(openFile)) fileWrite(openFile, "\r\n["..date.."]", content) end outputChatBox("#D16C00NEW UPDATES GOT ADDED!", getRootElement(), 255, 255, 255, true) outputChatBox("#D16C00- "..content, getRootElement(), 255, 255, 255, true) fileClose(openFile) end addEvent("COKupdates.addUpdate", true) addEventHandler("COKupdates.addUpdate", root, addUpdate) Now it writes a string after the original one. Edited May 12, 2017 by JAVAAA Link to comment
MTA Team botder Posted May 12, 2017 MTA Team Share Posted May 12, 2017 Get the entire file content after the position where you want to write your new string, then write your string and the file content afterwards. Link to comment
JAVAAA Posted May 12, 2017 Author Share Posted May 12, 2017 8 hours ago, Necktrox said: Get the entire file content after the position where you want to write your new string, then write your string and the file content afterwards. I didn't understand you actually. Link to comment
pa3ck Posted May 12, 2017 Share Posted May 12, 2017 Is there a specific position you will want to insert the new text or always before the old text, like [old_line1][old_line2] [ NEW LINE ] [old_line3] -> specific Or always before everything else? [NEW LINE][old_line1][old_line2] -> always there Link to comment
JAVAAA Posted May 12, 2017 Author Share Posted May 12, 2017 16 minutes ago, pa3ck said: Is there a specific position you will want to insert the new text or always before the old text, like [old_line1][old_line2] [ NEW LINE ] [old_line3] -> specific Or always before everything else? [NEW LINE][old_line1][old_line2] -> always there Before everything Link to comment
pa3ck Posted May 12, 2017 Share Posted May 12, 2017 Oh, okay, I haven't used files that much in MTA, but if you remove the fileSetPos @line 10 will it remove everything in the file? If so, you can do what @Necktrox said, load in everything from the file and append it after your new text. Link to comment
JAVAAA Posted May 12, 2017 Author Share Posted May 12, 2017 (edited) Doesn't work nvm, fixed Edited May 12, 2017 by JAVAAA 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