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