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

read file


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Sanjay Singh
Veteran Member
Join Date: Sep 2016
Old 01-07-2020 , 06:56   read file
Reply With Quote #1

How can i store lines data in array?
Its not working properly sometimes it just print 1st array or sometimes last.
Code

PHP Code:
new StoreData[128][128];
new 
testfile[255];

public 
loadStoreData()
{
    
get_configsdir(testfile,255)
    
format(testfile,255,"%s/test.ini",testfile)

    new 
line;
    new 
file fopen(testfile,"r")

    if(
file)
    {
        new 
data[128]
        while(
fgets(filedata127))
        {
            if(
data[0] == ';' || !data[0]) continue
            
parse(datadata127)

            
StoreData[line] = data
            line
++
        }
        
fclose(file)
    }

    for(new 
0sizeof linei++){ 
        
server_cmd("say %s"StoreData[i]);
    }

__________________

Last edited by Sanjay Singh; 01-07-2020 at 06:59.
Sanjay Singh is offline
Send a message via AIM to Sanjay Singh
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 01-07-2020 , 07:07   Re: read file
Reply With Quote #2

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)
    }

__________________

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

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()

        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)
    }

can you modify the current code instead of dynamic array bit easier for me.
__________________
Sanjay Singh is offline
Send a message via AIM to Sanjay Singh
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 01-07-2020 , 07:22   Re: read file
Reply With Quote #4

Easier for you doesn't mean more efficient. Not using dynamic array means that you must have a limited amount of space where you can store the lines. With dynamic arrays this space is, well, dynamic and you don't need to worry about anything failing.

It's really not complicated at all. You can use the code as is, but I suggest you use the array with a global variable.

PHP Code:
// this creates the array
// it's the same as "new StoreData[128][128]" you used, only here you're not limited to 128 lines
// you should define a global variable and then use ArrayCreate() inside plugin_init() or before reading the file
new Array:myArray ArrayCreate(128)

// this adds the string (the entire line) in the array
// same as using "StoreData[line++] = data"
ArrayPushString(myArrayszData)

// this gets the number of elements (strings) stored in the array
// same as using "sizeof(StoredData[])", although this one should be cached
ArraySize(myArray)

// this is a neat "trick" for retrieving a string from a dynamic array
// same as using %s + StoredData[i]
ArrayGetStringHandle(myArrayindex)

// the only difference is that you must destroy the array when you're done using it
// if you assigned it as a global variable, it's best to destroy it in plugin_end()
ArrayDestroy(myArray
__________________

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

Quote:
Originally Posted by OciXCrom View Post
Easier for you doesn't mean more efficient. Not using dynamic array means that you must have a limited amount of space where you can store the lines. With dynamic arrays this space is, well, dynamic and you don't need to worry about anything failing.

It's really not complicated at all. You can use the code as is, but I suggest you use the array with a global variable.

PHP Code:
// this creates the array
// it's the same as "new StoreData[128][128]" you used, only here you're not limited to 128 lines
// you should define a global variable and then use ArrayCreate() inside plugin_init() or before reading the file
new Array:myArray ArrayCreate(128)

// this adds the string (the entire line) in the array
// same as using "StoreData[line++] = data"
ArrayPushString(myArrayszData)

// this gets the number of elements (strings) stored in the array
// same as using "sizeof(StoredData[])", although this one should be cached
ArraySize(myArray)

// this is a neat "trick" for retrieving a string from a dynamic array
// same as using %s + StoredData[i]
ArrayGetStringHandle(myArrayindex)

// the only difference is that you must destroy the array when you're done using it
// if you assigned it as a global variable, it's best to destroy it in plugin_end()
ArrayDestroy(myArray
Oh Thanks for explaination , I will try this one.
__________________
Sanjay Singh is offline
Send a message via AIM to Sanjay Singh
Sanjay Singh
Veteran Member
Join Date: Sep 2016
Old 01-08-2020 , 06:29   Re: read file
Reply With Quote #6

Quote:
Originally Posted by Sanjay Singh View Post
How can i store lines data in array?
Its not working properly sometimes it just print 1st array or sometimes last.
Code

PHP Code:
new StoreData[128][128];
new 
testfile[255];

public 
loadStoreData()
{
    
get_configsdir(testfile,255)
    
format(testfile,255,"%s/test.ini",testfile)

    new 
line;
    new 
file fopen(testfile,"r")

    if(
file)
    {
        new 
data[128]
        while(
fgets(filedata127))
        {
            if(
data[0] == ';' || !data[0]) continue
            
parse(datadata127)

            
StoreData[line] = data
            line
++
        }
        
fclose(file)
    }

    for(new 
0sizeof linei++){ 
        
server_cmd("say %s"StoreData[i]);
    }

bdw whats error in this? why this one not working
__________________
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 , 07:44   Re: read file
Reply With Quote #7

1. You're using "parse" for no reason. See its documentation to understand what it does - https://amxx-bg.info/api/string/parse

2. In the for-loop, you're using "sizeof line" instead of "sizeof StoredData", so basically you're retrieving the size of an integer, which is always equal to 1.
__________________

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

Quote:
Originally Posted by OciXCrom View Post
1. You're using "parse" for no reason. See its documentation to understand what it does - https://amxx-bg.info/api/string/parse

2. In the for-loop, you're using "sizeof line" instead of "sizeof StoredData", so basically you're retrieving the size of an integer, which is always equal to 1.
fixed bdw what can be used then instead of parse?
__________________
Sanjay Singh is offline
Send a message via AIM to Sanjay Singh
Sanjay Singh
Veteran Member
Join Date: Sep 2016
Old 01-08-2020 , 08:34   Re: read file
Reply With Quote #9

How can i read separately for example;
StoreData1 and StoreDate2 by using this array method.
File: file.ini
PHP Code:
[StoreData1]
SD1.1
SD1.2

[StoreData2]
SD2.1
SD2.3 


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)
    }

__________________
Sanjay Singh is offline
Send a message via AIM to Sanjay Singh
^SmileY
Veteran Member
Join Date: Jan 2010
Location: Brazil [<o>]
Old 01-08-2020 , 09:09   Re: read file
Reply With Quote #10

I recommend you to use ini parser in this case.
__________________
Projects:

- See my Git Hub: https://github.com/SmileYzn
PHP Code:
set_pcvar_num(pCvar, !get_pcvar_num(pCvar)); 
^SmileY is offline
Send a message via MSN to ^SmileY Send a message via Skype™ to ^SmileY
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 23:06.


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