AlliedModders

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

Sanjay Singh 01-07-2020 06:56

read file
 
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]);
    }



OciXCrom 01-07-2020 07:07

Re: read file
 
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)
    }



Sanjay Singh 01-07-2020 07:11

Re: read file
 
Quote:

Originally Posted by OciXCrom (Post 2679241)
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.

OciXCrom 01-07-2020 07:22

Re: read file
 
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


Sanjay Singh 01-07-2020 07:57

Re: read file
 
Quote:

Originally Posted by OciXCrom (Post 2679244)
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 01-08-2020 06:29

Re: read file
 
Quote:

Originally Posted by Sanjay Singh (Post 2679238)
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

OciXCrom 01-08-2020 07:44

Re: read file
 
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.

Sanjay Singh 01-08-2020 08:13

Re: read file
 
Quote:

Originally Posted by OciXCrom (Post 2679384)
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 01-08-2020 08:34

Re: read file
 
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)
    }



^SmileY 01-08-2020 09:09

Re: read file
 
I recommend you to use ini parser in this case.


All times are GMT -4. The time now is 08:16.

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