File save/loading system
#10

Quote:
Originally Posted by iggy1
Посмотреть сообщение
I have an example of how you might do it. I know this isn't the best way to do it but it's the smallest i could think of.

pawn Код:
stock GetIniInt(FilePath[], Key[])
{
    if( fexist(FilePath) )
    {
        new
            pos,
            line[128],
            File: file_handle = fopen(FilePath, io_read)
        ;
        while( fread(file_handle, line) )//loop through the file
        {
            if( (pos = strfind(line, Key)) != -1 )//search the line for the Key
            {
                new len = pos + strlen(Key)+1;
                return (len<sizeof(line)) ? strval(line[len]) : 0;//return the converted value or zero if out of bounds
            }
        }
        fclose(file_handle);
    }
    return 0;
}
//To use

printf("TEST score == %d", GetIniInt("test.ini", "score"));
This isn't really safe to use for all types of ini file, it will only work if the ini format is "Key=Value" it will not work if the format is Key = Value (note the spaces). It also opens/closes the file each time it is called. For better ways of doing it you should look at some of the released file scripts, they will be much better than this.
Thanks you have given me some insight

Ill be looking at some file systems now such as dini and yini and see how they do it
Reply


Messages In This Thread
File save/loading system - by thefatshizms - 23.11.2012, 15:29
Re: File save/loading system - by dr.lozer - 23.11.2012, 15:57
Re: File save/loading system - by thefatshizms - 23.11.2012, 16:00
Re: File save/loading system - by dr.lozer - 23.11.2012, 16:11
Re: File save/loading system - by thefatshizms - 23.11.2012, 16:13
Re: File save/loading system - by dr.lozer - 23.11.2012, 16:23
Re: File save/loading system - by thefatshizms - 23.11.2012, 16:26
Re: File save/loading system - by dr.lozer - 23.11.2012, 16:30
Re: File save/loading system - by iggy1 - 23.11.2012, 17:09
Re: File save/loading system - by thefatshizms - 23.11.2012, 17:28

Forum Jump:


Users browsing this thread: 1 Guest(s)