23.11.2012, 17:09
(
Последний раз редактировалось iggy1; 23.11.2012 в 18:28.
Причина: Removed strmid, added bound check and closed file properly haha
)
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.
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.
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
{
fclose(file_handle);
new idx = pos + strlen(Key)+1;//+1 is for the equals sign
return (idx<sizeof(line)) ? strval(line[idx]) : 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"));