Raised This Month: $ Target: $400
 0% 

Solved Need Code For CFG FILE


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Begineer Programmer
Member
Join Date: Nov 2021
Old 07-24-2022 , 22:37   Re: Need Code For CFG FILE
Reply With Quote #1

Quote:
Originally Posted by fysiks View Post
If I thought he wasn't capable of doing this himself, I would have just done it (he said he wanted to learn).

Why did you add the "_amxx" to the cfg file? That will prevent this from working at all. Also, you should remove the break because if the cvar is defined twice or more in the config file then only the last one will actually be accurate.

The other thing is that just because a file exists doesn't mean it will be accessible. So, you don't need to see if it exists, just try to open if and if you don't get a valid file pointer then you can't execute any of the file reading code (this method is shown by the code I've referenced).
PHP Code:
get_map_maxrounds(szMapname[])
{
    new 
szConfigsDir[64], szFilePath[128]
    
get_configsdir(szConfigsDircharsmax(szConfigsDir))
    
format(szFilePathcharsmax(szFilePath), "%s/maps/%s.cfg",szConfigsDirszMapname)
    new 
fopen(szFilePath"rt")
    if( 
)
    {
        new 
szCvar[32], szValue[32], szCfgData[64],iValue;
        
        while(
fgets(fszCfgDatacharsmax(szCfgData)))
        {
            
parse(szCfgDataszCvarcharsmax(szCvar), szValuecharsmax(szValue));

            if(
equali(szCvar"mp_maxrounds"))
            {
                
iValue str_to_num(szValue)
                return 
iValue;
            }
        }
        
fclose(f)
    }
    

Quote:
Also, you should remove the break because if the cvar is defined twice or more in the config file then only the last one will actually be accurate.
check this one ?
Begineer Programmer is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 07-24-2022 , 22:43   Re: Need Code For CFG FILE
Reply With Quote #2

Quote:
Originally Posted by Begineer Programmer View Post
PHP Code:
get_map_maxrounds(szMapname[])
{
    new 
szConfigsDir[64], szFilePath[128]
    
get_configsdir(szConfigsDircharsmax(szConfigsDir))
    
format(szFilePathcharsmax(szFilePath), "%s/maps/%s.cfg",szConfigsDirszMapname)
    new 
fopen(szFilePath"rt")
    if( 
)
    {
        new 
szCvar[32], szValue[32], szCfgData[64],iValue;
        
        while(
fgets(fszCfgDatacharsmax(szCfgData)))
        {
            
parse(szCfgDataszCvarcharsmax(szCvar), szValuecharsmax(szValue));

            if(
equali(szCvar"mp_maxrounds"))
            {
                
iValue str_to_num(szValue)
                return 
iValue;
            }
        }
        
fclose(f)
    }
    


check this one ?
An important part of learning to code is to learn how to test and debug your own plugins. There very least that you could do is to test your code on your server to see if it works. It can be done really quickly and will tell you if the basics is working.

Regarding the code, it looks mostly correct. However, I would move the return to the end of the function so that it takes into account for cases where it's defined multiple times.

EDIT: You also can't return before fclose() because it will cause issues if you don't properly close the file (as pointed out by +ARUKARI-).
__________________

Last edited by fysiks; 07-24-2022 at 22:49.
fysiks 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:32.


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