AlliedModders

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

risontek22 09-01-2013 03:20

help with file
 
hello, i want to make log stock. i Tried make this but this doesnt work :( help pls
PHP Code:

new const g_constLogDirectory[]    = "addons/amxmodx/data/lol/log.txt";

public 
plugin_init()
{
    
lolfile_log(g_constLogDirectory,"HLDS Start....");
}

public 
lolfile_log( const file[], const log[] )
{
    new 
file_index fopen(g_constLogDirectory,"a+");

    new 
log_text[128],log_time[16];
    
    
get_time("%m/%d/%Y - %H:%M:%S",log_time,sizeof(log_time) - 1);
    
    
formatex(log_text,sizeof(log_text) - 1,"%s : %s^n",log_time,log);
    
    
fputs(file_index,log_text);
    
    
fclose(file_index);
    
    return 
0;



Black Rose 09-01-2013 08:40

Re: help with file
 
While a+ creates files it doesn't create folders. You'll have to use dir_exists() to check if the folder exists and use mkdir() to create it otherwise.

It's also bad to hardcode paths. You should really use get_localinfo along with "amxx_datadir".

You should also include a failsafe, just in case.
if ( ! file_index )
return;

Since you don't use the returned value you don't need to return 0 at the end. You can remove it or just leave it at "return", the result will be the same either way.

SpaWn2KiLl 09-01-2013 09:31

Re: help with file
 
Try this:

PHP Code:

new const g_constLog[]    = "log.txt";

public 
plugin_init()
{
    
lolfile_log(g_constLogDirectory,"HLDS Start....");
}

public 
lolfile_log( const file[], const log[] )
{
    new 
path[200]
    new 
newpath[200]

    
get_basedir(path,199

    
formatex(newpath,charsmax(newpath), "%s/data/gangsystem"path)

    if(!
dir_exists(newpath))
        
mkdir(newpath)

    new 
g_constLogDirectory[200]
    
formatex(g_constLogDirectorycharsmax(g_constLogDirectory), "%s/%s"newpathg_constLog)

    new 
file_index fopen(g_constLogDirectory,"at");

    new 
log_text[128],log_time[16];
    
    
get_time("%m/%d/%Y - %H:%M:%S",log_time,sizeof(log_time) - 1);
    
    
formatex(log_text,sizeof(log_text) - 1,"%s : %s^n",log_time,log);
    
    
fprintf(file_index log_text)
    
    
fclose(file_index);



risontek22 09-01-2013 09:38

Re: help with file
 
FIXED! Thanx Black Rose a LOT!
PHP Code:

#include <amxmodx>

new const g_constDirectory[]    = "addons/amxmodx/data/lol/";
new const 
g_constLogDirectory[]    = "addons/amxmodx/data/lol/log.txt";

public 
plugin_init()
{
    
lolfile_log(g_constLogDirectory,"HLDS Start....");
}


public 
lolfile_log( const file[], const log[] )
{
    if( 
dir_exists(g_constDirectory) == )
    {
        
mkdir(g_constDirectory);
    }
    
    new 
file_index fopen(g_constLogDirectory,"a+");
    
    if( ! 
file_index )
    {
        return 
1;
    }
    
    new 
log_text[128],log_time[25];
    
    
get_time("%d.%m. %Y - %H:%M:%S",log_time,sizeof(log_time) - 1);
        
    
formatex(log_text,sizeof(log_text) - 1,"%s : %s^n",log_time,log);
    
    
fputs(file_index,log_text);
        
    
fclose(file_index);
        
    return 
0;



risontek22 09-01-2013 09:40

Re: help with file
 
SpaWn2KiLl, :D :D you were late :D

SpaWn2KiLl 09-01-2013 09:41

Re: help with file
 
Quote:

Originally Posted by risontek22 (Post 2026182)
SpaWn2KiLl, :D :D you were late :D

No problem XD, my way works too... I was editing the code in the forum and not in notepad or amxx studio XD

Black Rose 09-01-2013 10:07

Re: help with file
 
Quote:

Originally Posted by SpaWn2KiLl (Post 2026184)
No problem XD, my way works too... I was editing the code in the forum and not in notepad or amxx studio XD

The problem with doing so is that you don't see the result. For example, the size of log_time is too small.

Quote:

Originally Posted by risontek22 (Post 2026179)
FIXED! Thanx Black Rose a LOT!

It's better, but not complete I would say.

const file[] is never used because you use the hardcoded variable g_constLogDirectory[] instead.

And again, you really should use "amxx_datadir", because some people actually moves it.
Do you want me to write the whole code?


All times are GMT -4. The time now is 19:05.

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