Raised This Month: $12 Target: $400
 3% 

read file


Post New Thread Reply   
 
Thread Tools Display Modes
Sanjay Singh
Veteran Member
Join Date: Sep 2016
Old 01-08-2020 , 09:47   Re: read file
Reply With Quote #11

Quote:
Originally Posted by ^SmileY View Post
I recommend you to use ini parser in this case.
Any example?
__________________
Sanjay Singh is offline
Send a message via AIM to Sanjay Singh
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 01-08-2020 , 10:05   Re: read file
Reply With Quote #12

Quote:
Originally Posted by Sanjay Singh View Post
fixed bdw what can be used then instead of parse?
Nothing. Delete the line, it serves no function here.

Sections:

PHP Code:
ReadFile()
{
    new 
szFilename[256]
    
get_configsdir(szFilenamecharsmax(szFilename))
    
add(szFilenamecharsmax(szFilename), "/FileName.ini")

    new 
iFilePointer fopen(szFilename"rt")

    if(
iFilePointer)
    {
        new 
szData[128]
        new Array:
myArray ArrayCreate(128)

        while(!
feof(iFilePointer))
        {
            
fgets(iFilePointerszDatacharsmax(szData))
            
trim(szData)

            switch(
szData[0])
            {
                case 
EOS';''#': continue
                case 
'[':
                {
                    
iSize strlen(szData)

                    if(
szData[iSize 1] == ']')
                    {
                        
szData[0] = ''
                        
szData[1] = ''
                        
trim(szData)

                        
console_print(0"Current section: %s"szData)
                    }
                    else continue
                }
                default:
                {
                    
ArrayPushString(myArrayszData)
                }
            }
        }

        for(new 
iArraySize(myArray); i++)
        {
            
server_cmd("say %a"ArrayGetStringHandle(myArrayi))
        }

        
ArrayDestroy(myArray)
        
fclose(iFilePointer)
    }

If you have settings in your file, this is how you would approach them:

Method 1: "setting" "value" "another value"

PHP Code:
new szSetting[32], szValue[32], szAnotherValue[32]
parse(szDataszSettingcharsmax(szSetting), szValuecharsmax(szValue), szAnotherValuecharsmax(szAnotherValue)) 
Method 2: "setting" = "value"

PHP Code:
new szSetting[32], szValue[32]
strtok(szDataszSettingcharsmax(szSetting), szValuecharsmax(szValue), '='
__________________

Last edited by OciXCrom; 01-08-2020 at 10:08.
OciXCrom is offline
Send a message via Skype™ to OciXCrom
Sanjay Singh
Veteran Member
Join Date: Sep 2016
Old 01-17-2020 , 12:49   Re: read file
Reply With Quote #13

Quote:
Originally Posted by OciXCrom View Post
By using dynamic arrays. This is the code I use in nearly all my plugins:

PHP Code:
ReadFile()
{
    new 
szFilename[256]
    
get_configsdir(szFilenamecharsmax(szFilename))
    
add(szFilenamecharsmax(szFilename), "/FileName.ini")

    new 
iFilePointer fopen(szFilename"rt")

    if(
iFilePointer)
    {
        new 
szData[128]
        new Array:
myArray ArrayCreate(128)

        while(!
feof(iFilePointer))
        {
            
fgets(iFilePointerszDatacharsmax(szData))
            
trim(szData)

            switch(
szData[0])
            {
                case 
EOS';''#': continue
                default:
                {
                    
ArrayPushString(myArrayszData)
                }
            }
        }

        for(new 
iArraySize(myArray); i++)
        {
            
server_cmd("say %a"ArrayGetStringHandle(myArrayi))
        }

        
ArrayDestroy(myArray)
        
fclose(iFilePointer)
    }

If i want to use array variable outside this func then i need to make global variable right?
PHP Code:
new Array:myArray ArrayCreate(128
__________________
Sanjay Singh is offline
Send a message via AIM to Sanjay Singh
Sanjay Singh
Veteran Member
Join Date: Sep 2016
Old 01-17-2020 , 13:02   Re: read file
Reply With Quote #14

PHP Code:
public plugin_precache()
{    
    for(new 
iArraySize(myArray); i++)
    {
        
precache_generic(ArrayGetStringHandle(myArrayi));
    }

is this correct way?
__________________
Sanjay Singh is offline
Send a message via AIM to Sanjay Singh
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 01-17-2020 , 13:28   Re: read file
Reply With Quote #15

Yes.
__________________
OciXCrom is offline
Send a message via Skype™ to OciXCrom
Sanjay Singh
Veteran Member
Join Date: Sep 2016
Old 01-18-2020 , 03:16   Re: read file
Reply With Quote #16

But how can i store that each section data to different arrays so can be used later. I dont want to store in same array.
__________________
Sanjay Singh is offline
Send a message via AIM to Sanjay Singh
Sanjay Singh
Veteran Member
Join Date: Sep 2016
Old 01-18-2020 , 03:17   Re: read file
Reply With Quote #17

[section1]
Data1
Date2
Data3

[Section2]
Data1
Data2
Data3

//Store section1 in arraysec1 and section2 in arraysec2
__________________
Sanjay Singh is offline
Send a message via AIM to Sanjay Singh
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 01-18-2020 , 07:14   Re: read file
Reply With Quote #18

I already told you how to find the current section in the last code I posted, so you should be able to figure out the rest.

Code:
case '[': {     iSize = strlen(szData)     if(szData[iSize - 1] == ']')     {         szData[0] = ''         szData[i - 1] = ''         trim(szData)         console_print(0, "Current section: %s", szData)     }     else continue } default: {     ArrayPushString(myArray, szData) }

If you can print out the section, you can definitely store it in a variable too, and then use that variable in the "default" case to do different things based on the current section. Again - check my plugins for a full example.
__________________

Last edited by OciXCrom; 01-18-2020 at 07:15.
OciXCrom is offline
Send a message via Skype™ to OciXCrom
Sanjay Singh
Veteran Member
Join Date: Sep 2016
Old 01-25-2020 , 03:20   Re: read file
Reply With Quote #19

How can i retrieve value

SHOW_XP = 1
__________________
Sanjay Singh is offline
Send a message via AIM to Sanjay Singh
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 01-25-2020 , 03:35   Re: read file
Reply With Quote #20

Quote:
Originally Posted by OciXCrom View Post

Method 1: "setting" "value" "another value"

PHP Code:
new szSetting[32], szValue[32], szAnotherValue[32]
parse(szDataszSettingcharsmax(szSetting), szValuecharsmax(szValue), szAnotherValuecharsmax(szAnotherValue)) 
Method 2: "setting" = "value"

PHP Code:
new szSetting[32], szValue[32]
strtok(szDataszSettingcharsmax(szSetting), szValuecharsmax(szValue), '='
__________________

Last edited by HamletEagle; 01-25-2020 at 03:35.
HamletEagle is offline
Reply


Thread Tools
Display Modes

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 21:44.


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