AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Writing to file help (https://forums.alliedmods.net/showthread.php?t=153887)

Desikac 03-30-2011 17:39

Writing to file help
 
I've made a plugin that writes player's times to a file when they complete a KZ map and displays it to players who type /top15 or /top.
But when a player completes a map twice, his name and time appear twice in the list. I want to replace the old line with the new one when that happens.

Wrote this code, but it often does not write anything at all:
PHP Code:

new omg[3][32]
new 
text[72][32]

TimerStopid //player finished the map
{
    
g_bStartedid ] = false;
    
    if ( 
g_bHasTimerid ] )
    {
        new 
name[32]
        
get_user_name(idname,31)
        
client_printidprint_chat"Zavrsio si mapu za %d:%s%d"g_iMinutesid ], g_iSecondsid ] < 10 "0" ""g_iSecondsid ] );
        
g_bHasTimerid ] = false;
        
format(text[id], 73"<br>%s -------- %d:%s%d"nameg_iMinutesid ], g_iSecondsid ] < 10 "0" ""g_iSecondsid ])
        
set_task(5.0"hook"id)
        
        
        new 
iFilePos;
            new 
szLineItem64 ];
    
            new 
iFileHandle fopen"Vremena.txt" "r+t" );

            
iFilePos 0;

            while ( !
feofiFileHandle ) )
            {
                
fgetsiFileHandle szLineItem charsmaxszLineItem ) );

                
iFilePos++;
        
                if(
containszLineItem name) != -)
                {
                    
write_file("Vremena.txt"text[id] , iFilePos );
                    
format(omg[id], 2"1")
                    break;
                }    
            
            }
        
fclose(iFileHandle);
        
        
set_task(2.0"obrisi"id)
            
        
        
    }
    return 
PLUGIN_HANDLED
}

public 
obrisi(id) {
    if(
equal(omg[id], "1")) { //don't like booleans :)
        
omg[id][0] = 0
        text
[id][0] = 0
        
return PLUGIN_HANDLED
    
}
    
write_file("Vremena.txt"text[id])
    
text[id][0] = 0

    
return PLUGIN_HANDLED



Nyuszy 03-31-2011 09:34

Re: Writing to file help
 
you can use read_file instead of fopen and fgets:

PHP Code:

new line_num file_size("vremena.txt"1)
new 
line[100], len
new bool:player_in_file false
for(new 0line_numi++)
{
    
read_file("vremena.txt"iline99len)
    if(
len && containi(linename) != -1)
    {
        
write_file("vremena.txt""your text"i)
        
player_in_file true
        
break
    }
}
if(
player_in_file == false)
{
    
write_file("vremena.txt""your text", -1)


and /top i the same for loop, but you read the line to an array instead to line

poz, lucky from kgb :D


All times are GMT -4. The time now is 14:29.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.