Raised This Month: $ Target: $400
 0% 

how can i read from file


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
.Dare Devil.
Veteran Member
Join Date: Sep 2010
Old 04-19-2013 , 13:07   Re: how can i read from file
Reply With Quote #1

Quote:
Originally Posted by Podarok View Post
Thanks a lot. But what if I have , lets say this , at the top of the file
Code:
// Costs File ini [This is COST1] #define COST1 63 [This is COST2] #define COST2 100 ...
And so on, will you code work?
PHP Code:
#include <amxmodx>

#define filename "addons/amxmodx/config/myfile.ini"

new Array:g_keynames
new Array:g_found
new Array:g_values
#define get_loaded_value(%1,%2,%3) ArrayGetString(g_values,%1,%2,%3)

new g_cost1
new g_cost2
new g_cost3
new g_cost4
new g_cost5

public plugin_precache()
{
    
g_keynames ArrayCreate(32,1)    
    
g_values ArrayCreate(32,1)    
    
g_found ArrayCreate(1,1)

    
// Adding your stuff for reading is easy, i give you examples
    
g_cost1 add_key_to_load"COST1" )
    
g_cost2 add_key_to_load"COST2" )
    
g_cost3 add_key_to_load"COST3" )
    
g_cost4 add_key_to_load"COST4" )
    
g_cost5 add_key_to_load"COST5" )

    
// Now lets load
    
load_file()

    
// and now lets get values
    
new loaded[32], cost1cost2cost3cost4cost5
    get_loaded_value
(g_cost1loaded31)
    
cost1 str_to_num(loaded)

    
get_loaded_value(g_cost2loaded31)
    
cost2 str_to_num(loaded)

    
get_loaded_value(g_cost3loaded31)
    
cost3 str_to_num(loaded)

    
get_loaded_value(g_cost4loaded31)
    
cost4 str_to_num(loaded)

    
get_loaded_value(g_cost5loaded31)
    
cost5 str_to_num(loaded)
    
client_print0print_chat"cost1:%d cost2:%d cost3:%d cost4:%d cost5:%d"cost1cost2cost3cost4cost5 )

    
// and there you go!
    
    // also if you want to load float value then 
    
new Float:fl_cost
    get_loaded_value
(g_cost1loaded31)
    
fl_cost str_to_float(loaded// only this native is different
    
client_print0print_chat"cost float:%f"fl_cost )
}

stock add_key_to_loadkeyname[] )
{
    
ArrayPushStringg_keynameskeyname)
    
ArrayPushStringg_values"0")
    
ArrayPushCellg_found0)
    return 
ArraySize(g_found) - 1
}

stock load_file()
{
    static 
abline[128], key[32], value[32]

    
// Lets open your file, inside of variable b will be file id what is open.
    
fopen(filename"rt")
    while (
&& !feof(b))
    {
        
// lets read one line, inside of variable line is your line
        
fgets(bline127)
        
replace(line127"^n""")
        if( 
line == '/' || line == '[' ) continue
        if( 
containline"#define") != -replace(line127"#define""")
        
        
// Lets find keyname and value
        // so for example your line look like this
        // COST1 1
        // there is " " space between key "COST" and "1"
        // next native will break line to 2 parts
        
strtok(linekey31value31'='1// changed here

        // Now lets check that is the key and value that what you want
        
for( 0ArraySize(g_keynames); a++ )
        {
            
// Already found for key that value?
            
if( ArrayGetCellg_found) == ) continue

            
// get key what for you want to value 
            
ArrayGetStringg_keynamesaline31)

            
// if readed key is same like your key inside of plugin then lets load value : )
            
if( equalkeyline) )
            {
                
ArraySetStringg_valuesavalue)
                
ArraySetCellg_founda1)
                break
            }
        }
    }
    
// We always close the file when we dont need him
    
if (bfclose(b)

here, now it will works both ways.

Last edited by .Dare Devil.; 04-19-2013 at 13:08.
.Dare Devil. is offline
Podarok
BANNED
Join Date: Jan 2011
Location: Narnia
Old 04-19-2013 , 13:59   Re: how can i read from file
Reply With Quote #2

Error :
Code:
 if( line == '/' || line == '[' ) continue

Array must be indexed (line)
I know it has to be easy to fix, but I am NOOB with ARRAYS, I have never worked with ARRAYS
Podarok is offline
.Dare Devil.
Veteran Member
Join Date: Sep 2010
Old 04-19-2013 , 14:15   Re: how can i read from file
Reply With Quote #3

Quote:
Originally Posted by Podarok View Post
Error :
Code:
 if( line == '/' || line == '[' ) continue

Array must be indexed (line)
I know it has to be easy to fix, but I am NOOB with ARRAYS, I have never worked with ARRAYS
PHP Code:
#include <amxmodx>

#define filename "addons/amxmodx/config/myfile.ini"

new Array:g_keynames
new Array:g_found
new Array:g_values
#define get_loaded_value(%1,%2,%3) ArrayGetString(g_values,%1,%2,%3)

new g_cost1
new g_cost2
new g_cost3
new g_cost4
new g_cost5

public plugin_precache()
{
    
g_keynames ArrayCreate(32,1)    
    
g_values ArrayCreate(32,1)    
    
g_found ArrayCreate(1,1)

    
// Adding your stuff for reading is easy, i give you examples
    
g_cost1 add_key_to_load"COST1" )
    
g_cost2 add_key_to_load"COST2" )
    
g_cost3 add_key_to_load"COST3" )
    
g_cost4 add_key_to_load"COST4" )
    
g_cost5 add_key_to_load"COST5" )

    
// Now lets load
    
load_file()

    
// and now lets get values
    
new loaded[32], cost1cost2cost3cost4cost5
    get_loaded_value
(g_cost1loaded31)
    
cost1 str_to_num(loaded)

    
get_loaded_value(g_cost2loaded31)
    
cost2 str_to_num(loaded)

    
get_loaded_value(g_cost3loaded31)
    
cost3 str_to_num(loaded)

    
get_loaded_value(g_cost4loaded31)
    
cost4 str_to_num(loaded)

    
get_loaded_value(g_cost5loaded31)
    
cost5 str_to_num(loaded)
    
client_print0print_chat"cost1:%d cost2:%d cost3:%d cost4:%d cost5:%d"cost1cost2cost3cost4cost5 )

    
// and there you go!
    
    // also if you want to load float value then 
    
new Float:fl_cost
    get_loaded_value
(g_cost1loaded31)
    
fl_cost str_to_float(loaded// only this native is different
    
client_print0print_chat"cost float:%f"fl_cost )
}

stock add_key_to_loadkeyname[] )
{
    
ArrayPushStringg_keynameskeyname)
    
ArrayPushStringg_values"0")
    
ArrayPushCellg_found0)
    return 
ArraySize(g_found) - 1
}

stock load_file()
{
    static 
abline[128], key[32], value[32]

    
// Lets open your file, inside of variable b will be file id what is open.
    
fopen(filename"rt")
    while (
&& !feof(b))
    {
        
// lets read one line, inside of variable line is your line
        
fgets(bline127)
        
replace(line127"^n""")
        if( 
line[0] == '/' || line[0] == '[' ) continue
        if( 
containline"#define") != -replace(line127"#define""")
        
        
// Lets find keyname and value
        // so for example your line look like this
        // COST1 1
        // there is " " space between key "COST" and "1"
        // next native will break line to 2 parts
        
strtok(linekey31value31'='1// changed here

        // Now lets check that is the key and value that what you want
        
for( 0ArraySize(g_keynames); a++ )
        {
            
// Already found for key that value?
            
if( ArrayGetCellg_found) == ) continue

            
// get key what for you want to value 
            
ArrayGetStringg_keynamesaline31)

            
// if readed key is same like your key inside of plugin then lets load value : )
            
if( equalkeyline) )
            {
                
ArraySetStringg_valuesavalue)
                
ArraySetCellg_founda1)
                break
            }
        }
    }
    
// We always close the file when we dont need him
    
if (bfclose(b)

Oh yeah, i always forgot them...
.Dare Devil. is offline
Podarok
BANNED
Join Date: Jan 2011
Location: Narnia
Old 04-20-2013 , 01:54   Re: how can i read from file
Reply With Quote #4

Doesnt work for me. Here is my code
PHP Code:
#include <amxmodx> 

#define filename "addons/amxmodx/configs/random_costs.ini" 

new Array:g_keynames 
new Array:g_found 
new Array:g_values 
#define get_loaded_value(%1,%2,%3) ArrayGetString(g_values,%1,%2,%3) 

new g_cost1 
new g_cost2 
new g_cost3 
new g_cost4 
new g_cost5 

#define NAME             "[] Test"
#define VERSION            "1.0"
#define AUTHOR            ".Dare Devil. & Podarok"

public plugin_init() 
{
    
register_plugin(NAMEVERSIONAUTHOR)
    
register_clcmd("say /checkit""checkit")
    
register_clcmd("checkit""checkit")
}

public 
plugin_precache() 

    
g_keynames ArrayCreate(32,1)     
    
g_values ArrayCreate(32,1)     
    
g_found ArrayCreate(1,1

    
// Now lets load 
    
load_file() 

    
// and there you go! 
     
    // also if you want to load float value then  
    /*new Float:fl_cost 
    get_loaded_value(g_cost1, loaded, 31) 
    fl_cost = str_to_float(loaded) // only this native is different 
    client_print( 0, print_chat, "cost float:%f", fl_cost ) */


public 
checkit(id)
{
    
// Adding your stuff for reading is easy, i give you examples 
    
g_cost1 add_key_to_load"COST1" 
    
g_cost2 add_key_to_load"COST2" 
    
g_cost3 add_key_to_load"COST3" 
    
g_cost4 add_key_to_load"COST4" 
    
g_cost5 add_key_to_load"COST5" 
    
    
// and now lets get values 
    
new loaded[32], cost1cost2cost3cost4cost5 
    get_loaded_value
(g_cost1loaded31
    
cost1 str_to_num(loaded
    
    
get_loaded_value(g_cost2loaded31
    
cost2 str_to_num(loaded
    
    
get_loaded_value(g_cost3loaded31
    
cost3 str_to_num(loaded
    
    
get_loaded_value(g_cost4loaded31
    
cost4 str_to_num(loaded
    
    
get_loaded_value(g_cost5loaded31
    
cost5 str_to_num(loaded
    
client_printidprint_chat"cost1:%d cost2:%d cost3:%d cost4:%d cost5:%d"cost1cost2cost3cost4cost5 
}

stock add_key_to_loadkeyname[] ) 

    
ArrayPushStringg_keynameskeyname
    
ArrayPushStringg_values"0"
    
ArrayPushCellg_found0
    return 
ArraySize(g_found) - 


stock load_file() 

    static 
abline[128], key[32], value[32

    
// Lets open your file, inside of variable b will be file id what is open. 
    
fopen(filename"rt"
    while (
&& !feof(b)) 
    { 
        
// lets read one line, inside of variable line is your line 
        
fgets(bline127
        
replace(line127"^n"""
        if( 
line[0] == '/' || line[0] == '[' || line[0] == ';') continue 
        if( 
containline"#define") != -replace(line127"#define"""
         
        
// Lets find keyname and value 
        // so for example your line look like this 
        // COST1 1 
        // there is " " space between key "COST" and "1" 
        // next native will break line to 2 parts 
        
strtok(linekey31value31'='1// changed here 

        // Now lets check that is the key and value that what you want 
        
for( 0ArraySize(g_keynames); a++ ) 
        { 
            
// Already found for key that value? 
            
if( ArrayGetCellg_found) == ) continue 

            
// get key what for you want to value  
            
ArrayGetStringg_keynamesaline31

            
// if readed key is same like your key inside of plugin then lets load value : ) 
            
if( equalkeyline) ) 
            { 
                
ArraySetStringg_valuesavalue
                
ArraySetCellg_founda1
                break 
            } 
        } 
    } 
    
// We always close the file when we dont need him 
    
if (bfclose(b

And the random_costs.ini file

Code:
; These are random costs [cost 1] COST1 = 1337 [cost2] COST2 = 128 [cost3] COST3 = 256 [cost4] COST4 = 5120 [cost5] COST5 = 123912
It shows cost 0 for all

Last edited by Podarok; 04-20-2013 at 03:18.
Podarok is offline
.Dare Devil.
Veteran Member
Join Date: Sep 2010
Old 04-20-2013 , 06:28   Re: how can i read from file
Reply With Quote #5

Quote:
Originally Posted by Podarok View Post
Doesnt work for me. Here is my code
PHP Code:
#include <amxmodx> 

#define filename "addons/amxmodx/configs/random_costs.ini" 

new Array:g_keynames 
new Array:g_found 
new Array:g_values 
#define get_loaded_value(%1,%2,%3) ArrayGetString(g_values,%1,%2,%3) 

new g_cost1 
new g_cost2 
new g_cost3 
new g_cost4 
new g_cost5 

#define NAME             "[] Test"
#define VERSION            "1.0"
#define AUTHOR            ".Dare Devil. & Podarok"

public plugin_init() 
{
    
register_plugin(NAMEVERSIONAUTHOR)
    
register_clcmd("say /checkit""checkit")
    
register_clcmd("checkit""checkit")
}

public 
plugin_precache() 

    
g_keynames ArrayCreate(32,1)     
    
g_values ArrayCreate(32,1)     
    
g_found ArrayCreate(1,1

    
// Now lets load 
    
load_file() 

    
// and there you go! 
     
    // also if you want to load float value then  
    /*new Float:fl_cost 
    get_loaded_value(g_cost1, loaded, 31) 
    fl_cost = str_to_float(loaded) // only this native is different 
    client_print( 0, print_chat, "cost float:%f", fl_cost ) */


public 
checkit(id)
{
    
// Adding your stuff for reading is easy, i give you examples 
    
g_cost1 add_key_to_load"COST1" 
    
g_cost2 add_key_to_load"COST2" 
    
g_cost3 add_key_to_load"COST3" 
    
g_cost4 add_key_to_load"COST4" 
    
g_cost5 add_key_to_load"COST5" 
    
    
// and now lets get values 
    
new loaded[32], cost1cost2cost3cost4cost5 
    get_loaded_value
(g_cost1loaded31
    
cost1 str_to_num(loaded
    
    
get_loaded_value(g_cost2loaded31
    
cost2 str_to_num(loaded
    
    
get_loaded_value(g_cost3loaded31
    
cost3 str_to_num(loaded
    
    
get_loaded_value(g_cost4loaded31
    
cost4 str_to_num(loaded
    
    
get_loaded_value(g_cost5loaded31
    
cost5 str_to_num(loaded
    
client_printidprint_chat"cost1:%d cost2:%d cost3:%d cost4:%d cost5:%d"cost1cost2cost3cost4cost5 
}

stock add_key_to_loadkeyname[] ) 

    
ArrayPushStringg_keynameskeyname
    
ArrayPushStringg_values"0"
    
ArrayPushCellg_found0
    return 
ArraySize(g_found) - 


stock load_file() 

    static 
abline[128], key[32], value[32

    
// Lets open your file, inside of variable b will be file id what is open. 
    
fopen(filename"rt"
    while (
&& !feof(b)) 
    { 
        
// lets read one line, inside of variable line is your line 
        
fgets(bline127
        
replace(line127"^n"""
        if( 
line[0] == '/' || line[0] == '[' || line[0] == ';') continue 
        if( 
containline"#define") != -replace(line127"#define"""
         
        
// Lets find keyname and value 
        // so for example your line look like this 
        // COST1 1 
        // there is " " space between key "COST" and "1" 
        // next native will break line to 2 parts 
        
strtok(linekey31value31'='1// changed here 

        // Now lets check that is the key and value that what you want 
        
for( 0ArraySize(g_keynames); a++ ) 
        { 
            
// Already found for key that value? 
            
if( ArrayGetCellg_found) == ) continue 

            
// get key what for you want to value  
            
ArrayGetStringg_keynamesaline31

            
// if readed key is same like your key inside of plugin then lets load value : ) 
            
if( equalkeyline) ) 
            { 
                
ArraySetStringg_valuesavalue
                
ArraySetCellg_founda1
                break 
            } 
        } 
    } 
    
// We always close the file when we dont need him 
    
if (bfclose(b

And the random_costs.ini file

Code:
; These are random costs [cost 1] COST1 = 1337 [cost2] COST2 = 128 [cost3] COST3 = 256 [cost4] COST4 = 5120 [cost5] COST5 = 123912
It shows cost 0 for all
put load_file() always there where you add keys to load
like this
PHP Code:
    g_cost1 add_key_to_load"COST1" 
    
g_cost2 add_key_to_load"COST2" 
    
g_cost3 add_key_to_load"COST3" 
    
g_cost4 add_key_to_load"COST4" 
    
g_cost5 add_key_to_load"COST5" 
    
load_file() 
problem is that it read file before my script know what to read...
make sure that always is load_file() after add_key_to_loads
.Dare Devil. is offline
Podarok
BANNED
Join Date: Jan 2011
Location: Narnia
Old 04-20-2013 , 06:42   Re: how can i read from file
Reply With Quote #6

Thanks, but is it then necessary to do this in plugin precache?
Code:
g_keynames = ArrayCreate(32,1) g_values = ArrayCreate(32,1) g_found = ArrayCreate(1,1) // Now lets load load_file()
Do I have to load file twise?
Podarok is offline
.Dare Devil.
Veteran Member
Join Date: Sep 2010
Old 04-20-2013 , 06:58   Re: how can i read from file
Reply With Quote #7

Quote:
Originally Posted by Podarok View Post
Thanks, but is it then necessary to do this in plugin precache?
Code:
g_keynames = ArrayCreate(32,1) g_values = ArrayCreate(32,1) g_found = ArrayCreate(1,1) // Now lets load load_file()
Do I have to load file twise?
those ArrayCreate(32,1) must they there but load_file will go there where i told you, do not load twice.
.Dare Devil. is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 04-20-2013 , 07:12   Re: how can i read from file
Reply With Quote #8

May this help you

PHP Code:
#include <amxmodx>
#include <amxmisc>

enum dataType
{
    
INTEGER,
    
FLOAT,
    
VECTOR,
    
STRING
}

new 
Trie:g_tDatasTypes;
new 
Trie:g_tDatasValues;

new 
g_szName[32], Float:g_flGravityFloat:g_flOrigin[3];

public 
plugin_init()
{
    new 
cfgFile[128];
    
get_configsdir(cfgFilecharsmax(cfgFile));
    
add(cfgFilecharsmax(cfgFile), "/my_file.ini");

    
// set data types
    
DataPreset();

    
// set values
    
LoadFromFile(cfgFile);

    
// retrieve values and set our variables
    
TrieGetString(g_tDatasValues"Name"g_szNamecharsmax(g_szName));
    
TrieGetCell(g_tDatasValues"Gravity"g_flGravity);
    
TrieGetArray(g_tDatasValues"Origin"g_flOriginsizeof(g_flOrigin));
}

DataPreset()
{
    
g_tDatasTypes TrieCreate();
    
g_tDatasValues TrieCreate();
    
TrieSetCell(g_tDatasTypes"Name"STRING);
    
TrieSetCell(g_tDatasTypes"Gravity"FLOAT);
    
TrieSetCell(g_tDatasTypes"Origin"VECTOR);
}

LoadFromFileszFile[] )
{
    new 
fp fopen(szFile"rt");
    if( 
fp )
    {
        new 
buffer[256], key[16], ctypeargs[3][32];
        while( !
feof(fp) )
        {
            
fgets(fpbuffercharsmax(buffer));
            
trim(buffer);
            
buffer[0];
            if( 
&& != '#' && != ';' && !( == '/' && buffer[1] == '/' ) )
            {
                
strtok(bufferkeycharsmax(key), buffercharsmax(buffer));
                if( 
TrieGetCell(g_tDatasTypeskeytype) )
                {
                    
parse(bufferargs[0], charsmax(args[]), args[1], charsmax(args[]), args[2], charsmax(args[]));
                    switch( 
type )
                    {
                        case 
INTEGER:
                        {
                            
TrieSetCell(g_tDatasValueskeystr_to_num(args[0]));
                        }
                        case 
FLOAT:
                        {
                            
TrieSetCell(g_tDatasValueskeystr_to_float(args[0]));
                        }
                        case 
VECTOR:
                        {
                            new 
Float:vector[3];
                            
vector[0] = str_to_float(args[0]);
                            
vector[1] = str_to_float(args[1]);
                            
vector[2] = str_to_float(args[2]);
                            
TrieSetArray(g_tDatasValueskeyvectorsizeof(vector));
                        }
                        case 
STRING:
                        {
                            
TrieSetString(g_tDatasValueskeyargs[0]);
                        }
                    }
                }
            }
        }
        
fclosefp );
        
fp 0;
    }

__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
.Dare Devil.
Veteran Member
Join Date: Sep 2010
Old 04-20-2013 , 07:23   Re: how can i read from file
Reply With Quote #9

Quote:
Originally Posted by ConnorMcLeod View Post
May this help you

PHP Code:
#include <amxmodx>
#include <amxmisc>

enum dataType
{
    
INTEGER,
    
FLOAT,
    
VECTOR,
    
STRING
}

new 
Trie:g_tDatasTypes;
new 
Trie:g_tDatasValues;

new 
g_szName[32], Float:g_flGravityFloat:g_flOrigin[3];

public 
plugin_init()
{
    new 
cfgFile[128];
    
get_configsdir(cfgFilecharsmax(cfgFile));
    
add(cfgFilecharsmax(cfgFile), "/my_file.ini");

    
// set data types
    
DataPreset();

    
// set values
    
LoadFromFile(cfgFile);

    
// retrieve values and set our variables
    
TrieGetString(g_tDatasValues"Name"g_szNamecharsmax(g_szName));
    
TrieGetCell(g_tDatasValues"Gravity"g_flGravity);
    
TrieGetArray(g_tDatasValues"Origin"g_flOriginsizeof(g_flOrigin));
}

DataPreset()
{
    
g_tDatasTypes TrieCreate();
    
g_tDatasValues TrieCreate();
    
TrieSetCell(g_tDatasTypes"Name"STRING);
    
TrieSetCell(g_tDatasTypes"Gravity"FLOAT);
    
TrieSetCell(g_tDatasTypes"Origin"VECTOR);
}

LoadFromFileszFile[] )
{
    new 
fp fopen(szFile"rt");
    if( 
fp )
    {
        new 
buffer[256], key[16], ctypeargs[3][32];
        while( !
feof(fp) )
        {
            
fgets(fpbuffercharsmax(buffer));
            
trim(buffer);
            
buffer[0];
            if( 
&& != '#' && != ';' && !( == '/' && buffer[1] == '/' ) )
            {
                
strtok(bufferkeycharsmax(key), buffercharsmax(buffer));
                if( 
TrieGetCell(g_tDatasTypeskeytype) )
                {
                    
parse(bufferargs[0], charsmax(args[]), args[1], charsmax(args[]), args[2], charsmax(args[]));
                    switch( 
type )
                    {
                        case 
INTEGER:
                        {
                            
TrieSetCell(g_tDatasValueskeystr_to_num(args[0]));
                        }
                        case 
FLOAT:
                        {
                            
TrieSetCell(g_tDatasValueskeystr_to_float(args[0]));
                        }
                        case 
VECTOR:
                        {
                            new 
Float:vector[3];
                            
vector[0] = str_to_float(args[0]);
                            
vector[1] = str_to_float(args[1]);
                            
vector[2] = str_to_float(args[2]);
                            
TrieSetArray(g_tDatasValueskeyvectorsizeof(vector));
                        }
                        case 
STRING:
                        {
                            
TrieSetString(g_tDatasValueskeyargs[0]);
                        }
                    }
                }
            }
        }
        
fclosefp );
        
fp 0;
    }

what is wrong with cellarray?
.Dare Devil. is offline
Podarok
BANNED
Join Date: Jan 2011
Location: Narnia
Old 04-20-2013 , 13:51   Re: how can i read from file
Reply With Quote #10

Dare , this does not work :/

PHP Code:
#include <amxmodx> 

#define filename "addons/amxmodx/configs/random_costs.ini" 

new Array:g_keynames 
new Array:g_found 
new Array:g_values 
#define get_loaded_value(%1,%2,%3) ArrayGetString(g_values,%1,%2,%3) 

new g_cost1 
new g_cost2 
new g_cost3 
new g_cost4 
new g_cost5 

#define NAME             "[] Test"
#define VERSION            "1.0"
#define AUTHOR            ".Dare Devil. & Podarok"

public plugin_init() 
{
    
register_plugin(NAMEVERSIONAUTHOR)
    
register_clcmd("say /checkit""checkit")
    
register_clcmd("checkit""checkit")
}

public 
plugin_precache() 

    
g_keynames ArrayCreate(32,1)     
    
g_values ArrayCreate(32,1)     
    
g_found ArrayCreate(1,1

    
// Now lets load 

    // and there you go! 
     
    // also if you want to load float value then  
    /*new Float:fl_cost 
    get_loaded_value(g_cost1, loaded, 31) 
    fl_cost = str_to_float(loaded) // only this native is different 
    client_print( 0, print_chat, "cost float:%f", fl_cost ) */


public 
checkit(id)
{
    
// Adding your stuff for reading is easy, i give you examples 
    
g_cost1 add_key_to_load"COST1" 
    
g_cost2 add_key_to_load"COST2" 
    
g_cost3 add_key_to_load"COST3" 
    
g_cost4 add_key_to_load"COST4" 
    
g_cost5 add_key_to_load"COST5" 
    
    
load_file() 
    
    
// and now lets get values 
    
new loaded[32], cost1cost2cost3cost4cost5 
    get_loaded_value
(g_cost1loaded31
    
cost1 str_to_num(loaded
    
    
get_loaded_value(g_cost2loaded31
    
cost2 str_to_num(loaded
    
    
get_loaded_value(g_cost3loaded31
    
cost3 str_to_num(loaded
    
    
get_loaded_value(g_cost4loaded31
    
cost4 str_to_num(loaded
    
    
get_loaded_value(g_cost5loaded31
    
cost5 str_to_num(loaded
    
client_printidprint_chat"cost1:%d cost2:%d cost3:%d cost4:%d cost5:%d"cost1cost2cost3cost4cost5 
}

stock add_key_to_loadkeyname[] ) 

    
ArrayPushStringg_keynameskeyname
    
ArrayPushStringg_values"0"
    
ArrayPushCellg_found0
    return 
ArraySize(g_found) - 


stock load_file() 

    static 
abline[128], key[32], value[32

    
// Lets open your file, inside of variable b will be file id what is open. 
    
fopen(filename"rt"
    while (
&& !feof(b)) 
    { 
        
// lets read one line, inside of variable line is your line 
        
fgets(bline127
        
replace(line127"^n"""
        if( 
line[0] == '/' || line[0] == '[' || line[0] == ';') continue 
        if( 
containline"#define") != -replace(line127"#define"""
         
        
// Lets find keyname and value 
        // so for example your line look like this 
        // COST1 1 
        // there is " " space between key "COST" and "1" 
        // next native will break line to 2 parts 
        
strtok(linekey31value31'='1// changed here 

        // Now lets check that is the key and value that what you want 
        
for( 0ArraySize(g_keynames); a++ ) 
        { 
            
// Already found for key that value? 
            
if( ArrayGetCellg_found) == ) continue 

            
// get key what for you want to value  
            
ArrayGetStringg_keynamesaline31

            
// if readed key is same like your key inside of plugin then lets load value : ) 
            
if( equalkeyline) ) 
            { 
                
ArraySetStringg_valuesavalue
                
ArraySetCellg_founda1
                break 
            } 
        } 
    } 
    
// We always close the file when we dont need him 
    
if (bfclose(b

My ini file

Code:
; These are random costs [cost 1] COST1 = 1337 [cost2] COST2 = 128 [cost3] COST3 = 256 [cost4] COST4 = 5120 [cost5] COST5 = 123912

It shows 0 for all

Last edited by Podarok; 04-20-2013 at 14:12.
Podarok is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 10:56.


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