AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   reading a file. (https://forums.alliedmods.net/showthread.php?t=134941)

drekes 08-10-2010 13:09

reading a file.
 
I'm having troubles reading a file, i looked into other codes but can't seem to find the answer.
It should check if the player's steamid is in the file on disconnect , and replace that line with a new one, or add a line when he's not in the file.

The problem is that it crashes my game when i disconnect.

PHP Code:

#pragma semicolon 1

#include <amxmodx>
#include <amxmisc>

#define VERSION "1.0"

new file[128];
new 
currentmap[64];

public 
plugin_init()
{
    
register_plugin("Player's time logger"VERSION"Drekes");
    
    
// Start the log file.
    
new currentdate[15];
    
get_time("%d"currentdatecharsmax(currentdate));
    
    new 
dir[64];
    
get_datadir(dircharsmax(dir));
    
    
format(dircharsmax(dir), "%s/timelogs"dir);
    
    if(!
dir_exists(dir))
        
mkdir(dir);
    
    
formatex(filecharsmax(file), "%s/log-%s.ini"dircurrentdate);

    
get_mapname(currentmapcharsmax(currentmap));
    
    new 
mapchangeline[128];
    
formatex(mapchangelinecharsmax(mapchangeline), "*-- Mapchange to %s --*"currentmap);
    
    
write_file(filemapchangeline);
}

public 
client_disconnect(id)
{
    new 
playtime get_user_time(id) / 60;
    
    new 
authid[35];
    
get_user_authid(idauthidcharsmax(authid));
    
    new 
data[128], filepos;
    new 
fileid[35], filetime[20];
    
    new 
writefile fopen(file"rw");
    
    if(
writefile)
    {
        while(!
feof(writefile))
        {
            
fgets(writefiledatacharsmax(data));
            
            
filepos++;
            
            if(
data[0] == '*')
                continue;
                
            
parse(datafileidcharsmax(fileid), filetimecharsmax(filetime));
            
            if(
equali(authidfileid))
            {
                new 
filetimenum str_to_num(filetime);
                                
                new 
writetime playtime filetimenum;
                
                
formatex(datacharsmax(data), "%s %i"authidwritetime);
                
                break;
            }
    
            
formatex(datacharsmax(data), "%s %i"authidplaytime);
        }
    
        
write_file(filedata);
    }
    
    
fclose(writefile);



ConnorMcLeod 08-10-2010 13:13

Re: reading a file.
 
data array seems a bit small ( 128 )
You should write the new file with new file system, lines after lines.

drekes 08-10-2010 13:14

Re: reading a file.
 
Thanks for the fast reply.
How can you see this so fast, so i can avoid it later?

Edit: Could you explain a bit about that "lines after lines", I'm not really sure what you mean by that.

drekes 08-10-2010 18:44

Re: reading a file.
 
I figured it out with some help by wrecked, thanks for your help connor.


All times are GMT -4. The time now is 21:55.

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