How to get a specific value of a key from a file?
#1

Hi, I've just found that function but it's not working.
Can someone fix it?
PHP код:
stock INI_Get(filename[], key[])
{
    new 
File:F,string[128];
    new 
sname[24],sval[24];
    
fopen(filename,io_read);
    if(!
F) return sname;
    while(
fread(F,string))
    {
        
sscanf(string,"p<=>s[24]s[24]",sname,sval);
        if(!
strcmp(sname,key))
        {
            
sval[strlen(sval)-2] = 0;
            
fclose(F);
            return 
sval;
        }
    }
    
fclose(F);
    
sname[0] = '\0';
    return 
sname;

PHP код:
stock INI_GetValue(filename[], key[])
{
    return 
strval(INI_Get(filenamekey));

For example I do:

PHP код:
if(fexist("text.txt"))
    {
        
printf("%d"INI_GetValue("text.txt""Skills"));
    }
    else
    {
        print(
"File does not exist");
    } 
It doesn't work. Can someone help me? I really need that function.
Reply
#2

Try remove this line.
sval[strlen(sval)-2] = 0;

And show please your ini file.
Reply
#3

Quote:
Originally Posted by Borg
Посмотреть сообщение
Try remove this line.
sval[strlen(sval)-2] = 0;

And show please your ini file.
Oh thank you so much I removed that line and worked! Thank you!

EDIT:

It worked if in the file:

PHP код:
Skills=23 
But It doesn't work if in the file:

PHP код:
Skills 23 
Can you help me with that?
Reply
#4

pawn Код:
stock INI_Get(filename[], key[])
{
    new File:F,string[128];
    new sname[24],sval[24];
    F = fopen(filename,io_read);
    if(!F) return sname;
    while(fread(F,string))
    {
        sscanf(string,"p<=>s[24]s[24]",sname,sval);
        new l  = strlen(sname);
        new i  = 1;
        while(sname[l-i] == ' ') sname[l-i++] = 0;
        if(!strcmp(sname,key))
        {
            i = 0;
            while(sval[i] == ' ') i++;
            strdel(sval, 0, i);
            fclose(F);
            return sval;
        }
    }
    fclose(F);
    sname[0] = '\0';
    return sname;
}
but ini file should be without spaces.
Reply
#5

I'm using YINI and it writes with spaces.. Is there any way to make it read with spaces as the YINI functions do?

EDIT:

It worked with spaces too thank you so much.
Reply
#6

Does my second example work?

second example should work with spaces

you are always welcome
Reply
#7

Yes, it worked.
Thank you.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)