Jump to content

output the looped into a file?


Andreas.

Recommended Posts

Posted

hey, how can I output the looped into a file? I mean like

function lol() 
    for i,v in ipairs (getElementsByType ("player")) do 
    local theirdata = getElementData(v, "data") 
        thefile = fileCreate ("lol.txt") 
         fileWrite(thefile, theirdata) 
         fileClose(thefile) 
    end 
end 
addCommandHandler("loop", loll) 

Posted
I don't get what do you want to do.

I want to put all players data, a new row in the .txt for every output

like

content of lol.txt

player1data

player2data

player3data

and so on, you get me now? :P

Posted (edited)
function lol ( ) 
    local thefile = fileCreate ( "lol.txt" ) 
    for index, player in ipairs ( getElementsByType ( "player" ) ) do 
        local theirdata = getElementData ( player, "data" ) 
        fileWrite ( thefile, "\n\n".. getPlayerName ( player ) .." - ".. theirdata ) 
    end 
    fileClose ( thefile ) 
end 
addCommandHandler ( "loop", lol ) 

That?

Edited by Guest
Posted
function printAllData ( thePlayer ) 
    local playerAccount = getPlayerAccount( thePlayer ) -- get his account 
    if ( playerAccount ) then -- if we got the account then 
        local data = getAllAccountData( playerAccount ) -- get data 
        count = 0 
        for _ in pairs(data) do count = count + 1 end -- get the count 
        outputChatBox ( "table holds " .. count .. " entries" ) -- output number of rows 
        if ( data ) then 
            for k,v in pairs ( data ) do 
                accountLog = fileCreate("accounts/"..getAccountName(playerAccount)) 
                outputChatBox(k.." :"..v) 
                fileWrite ( accountLog, "\n\n"..k.." : ".. v ) 
            end 
                fileClose(accountLog) 
        end 
    end 
end 
addCommandHandler ( "loopx", printAllData ) 

that only prints:

  
  
weaponAmmo8 : 0 

when its supposed to print:

table holds 42 entries 
weaponAmmo12 :0 
weaponID10 :0 
weaponID12 :0 
weaponAmmo1 :0 
skin :0 
armor :0 
weaponAmmo3 :0 
weaponAmmo10 :0 
y :-1412 
x :2036 
dim :0 
z :12 
weaponAmmo9 :0 
B :107 
wantedlevel :0 
weaponAmmo7 :0 
weaponID4 :0 
weaponID6 :0 
weaponAmmo5 :0 
weaponID9 :0 
money :0 
weaponID7 :0 
weaponID5 :0 
weaponID3 :0 
weaponID2 :0 
weaponAmmo11 :0 
weaponAmmo0 :1 
R :237 
weaponAmmo4 :0 
G :106 
weaponAmmo6 :0 
Staff :Yes 
int :0 
weaponID11 :0 
health :100.3921661377 
weaponID1 :0 
weaponID0 :0 
weaponAmmo2 :0 
weaponID8 :0 
weaponAmmo8 :0 

Posted

That's because you are creating the file every time, you should have put the fileCreate function outside the loop.

function printAllData ( thePlayer ) 
    local playerAccount = getPlayerAccount ( thePlayer ) 
    if ( playerAccount ) then 
        local data = getAllAccountData ( playerAccount ) 
        local count = 0 
        if ( data ) then 
            local accountLog = fileCreate ( "accounts/".. getAccountName ( playerAccount ) ..".log" ) 
            for k, v in pairs ( data ) do 
                count = ( count + 1 ) 
                outputChatBox ( "\n".. k .." :".. v ) 
                fileWrite ( accountLog, "\n".. k ..": ".. v ) 
            end 
            outputChatBox ( "table holds ".. count .." entries" ) 
            fileClose ( accountLog ) 
        end 
    end 
end 
addCommandHandler ( "loopx", printAllData ) 

Posted
Well, I used that same code and it printed it like it does on chatbox, I didn't use notepad to open it though.

lol, that was the problem. Really thank you!

Posted

Whatever you're doing, if you plan on actually using this data, you're doing it in the worst way. XML is much easier to read and write, SQL is quicker and more flexible. Pure file IO functions aren't meant for storing this kind of thing.

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