Jump to content

[HELP] fileCreate


Enargy,

Recommended Posts

Hello, my mistake is that when I run the command by typing a text file created in the plogs.txt but if i write again this replaces the old text. I want to know is how do to add new text without deleting the old one.

server.lua

function createLogs(thePlayer, command, text) 
    local time = getRealTime() 
        dia  = time.hour     
        mes  = time.month 
        anio  = time.year    
     
    local fileHandle = fileCreate(":"..getResourceName(getThisResource()).."/txt/plogs.txt")   
    if fileHandle then 
        fileWrite(fileHandle, "* ["..dia.."/"..mes.."/"..anio.."] nickname = '"..getPlayerName(thePlayer).."'    text = '"..text.."' \n") 
        fileClose(fileHandle) 
    end 
end 
addCommandHandler("addtext", createLogs) 

Regards.

Link to comment
  • Administrators

That's because every time you run the command you are creating a new file-. Use this:

function createLogs(thePlayer, command, text) 
    local time = getRealTime() 
        dia  = time.hour    
        mes  = time.month 
        anio  = time.year    
   if not fileOpen (":"..getResourceName(getThisResource()).."/txt/plogs.txt") then 
    local fileHandle = fileCreate(":"..getResourceName(getThisResource()).."/txt/plogs.txt") 
    local fileread = fileOpen (":"..getResourceName(getThisResource()).."/txt/plogs.txt") 
   else 
   local fileread = fileOpen (":"..getResourceName(getThisResource()).."/txt/plogs.txt") 
   end 
    if fileread then 
        fileWrite(fileread, "* ["..dia.."/"..mes.."/"..anio.."] nickname = '"..getPlayerName(thePlayer).."'    text = '"..text.."' \n") 
        fileClose(fileread) 
    end 
end 
addCommandHandler("addtext", createLogs) 

Link to comment

Actually yes, it's exactly what Ciber told you above, though he's done few mistakes on the code he's given you.

Anyway, it's as simple as:

  
function createLogs(thePlayer, command, text) 
    local time = getRealTime() 
    local dia  = time.hour 
    local mes  = time.month 
    local anio  = time.year 
    local dir = "txt/plogs.txt" 
    local fileHandle = fileOpen(dir) or fileCreate(dir) 
    if fileHandle then 
        local content = fileRead(fileHandle, fileGetSize(fileHandle)) or "" 
        fileWrite(fileHandle, content, "* ["..dia.."/"..mes.."/"..anio.."] nickname = '"..getPlayerName(thePlayer).."'    text = '"..text.."' \n") 
        fileClose(fileHandle) 
    end 
end 
addCommandHandler("addtext", createLogs) 
  

Link to comment
Actually yes, it's exactly what Ciber told you above, though he's done few mistakes on the code he's given you.

Anyway, it's as simple as:

  
function createLogs(thePlayer, command, text) 
    local time = getRealTime() 
    local dia  = time.hour 
    local mes  = time.month 
    local anio  = time.year 
    local dir = "txt/plogs.txt" 
    local fileHandle = fileOpen(dir) or fileCreate(dir) 
    if fileHandle then 
        local content = fileRead(fileHandle, fileGetSize(fileHandle)) or "" 
        fileWrite(fileHandle, content, "* ["..dia.."/"..mes.."/"..anio.."] nickname = '"..getPlayerName(thePlayer).."'    text = '"..text.."' \n") 
        fileClose(fileHandle) 
    end 
end 
addCommandHandler("addtext", createLogs) 
  

it doesn't work. creating the file shows the following error:

WARNING: [server]\log\server.lua:127: fileOpen: unable to load file 
WARNING: [server]\log\server.lua:129: Bad usage @ 'fileRead' [bad number of bytes] 

the file is created with the first text but to repeat the procedure as it is not added the new text.

Link to comment
  
function createLogs(thePlayer, command, text) 
    local time = getRealTime() 
    local dia  = time.hour 
    local mes  = time.month 
    local anio  = time.year 
    local dir = "txt/plogs.txt" 
    local fileHandle = ((fileExists(dir) and fileOpen(dir)) or fileCreate(dir)) 
    if fileHandle then 
        local content = (fileRead(fileHandle, (fileGetSize(fileHandle) or 0)) or "") 
        fileWrite(fileHandle, content, "* ["..dia.."/"..mes.."/"..anio.."] nickname = '"..getPlayerName(thePlayer).."'    text = '"..text.."' \n") 
        fileClose(fileHandle) 
    end 
end 
addCommandHandler("addtext", createLogs) 
  

Link to comment
  • Administrators

Well, here is an example from a script i wrote to log chat messages to a file.

Adapt it to your needs:

--[[ 
  
* zGx - Zone Gaming Community 2014 
* Programado por CiBeR 
* Todos los derechos reservados.  
* Prohibido su uso fuera del servidor Oficial zGx. 
  
Modulo: Server - Log Chat 
  
]]-- 
  
function log_chat(message, messageType) 
    if not fileExists ( "logs/log_chat.log" ) then 
    fileCreate("logs/log_chat.log") 
    end 
    local log = fileOpen("logs/log_chat.log") 
    local size = fileGetSize ( log ) 
    local place = fileSetPos(log,size) 
    local name = getPlayerName(source):gsub ( "#%x%x%x%x%x%x", "" ) 
    local clan = getPlayerTeam(source)or"Ninguno" 
    local clanName = getTeamName(clan)or"Ninguno" 
    local fecha = getRealTime() 
    local an = fecha.year+1900 
    local mes = fecha.month+1 
    local fec = ""..string.format("%02d", fecha.monthday).."/"..string.format("%02d", mes).."/"..an.." "..string.format("%02d", fecha.hour)..":"..string.format("%02d", fecha.minute)..":"..string.format("%02d", fecha.second) 
        if messageType == 0 then 
        fileWrite(log, "["..fec.."][CHAT: "..name.." Dijo: "..message.." |CLAN: "..clanName.." ]\n")  
        fileClose(log) 
        elseif messageType == 2 then 
        fileWrite(log, "["..fec.."][TEAMCHAT: "..name.." Dijo: "..message.." |CLAN: "..clanName.." ]\n")  
        fileClose(log) 
        end 
end 
addEventHandler("onPlayerChat",root,log_chat) 

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