Getting an int from a file. - Printable Version
+- SA-MP Forums Archive (
https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (
https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (
https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Getting an int from a file. (
/showthread.php?tid=432025)
Getting an int from a file. -
thefatshizms - 20.04.2013
pawn Код:
stock INI_GetInt(INI:filehandle, Key[], &dest) {
if(!filehandle) return printf("Failed to open file!");
new pos, line[MAX_FILE_STRING], temp[MAX_FILE_STRING];
while( fread(filehandle, line) ) {
if( (pos = strfind(line, Key)) != -1 ) {//it's found the key
new idx = pos + strlen(Key)+1;
strmid(temp, line[pos], 0, idx);
if(!strcmp(temp[idx], " ", true)) {//if there is a space the strings won't match later on
strmid(temp, line, 0, idx-1);
}
if(!strcmp(temp, Key, true)) {//If there is something like Score and PlayersScore strfind won't work
if(line[idx] == '=') {//the value is a assignment
idx = pos + strlen(Key) + 2;//if the = sign is further across
return dest = (idx < sizeof(line)) ? strval(line[idx]) : 0;
}
}
}
}
return 1;
}
It works.. but I'm testing to make sure if there is something like below; I want to get the value of score (5) but it returns scores
Re: Getting an int from a file. -
zDivine - 20.04.2013
Nevermind, thought you were using Y_INI...
Re: Getting an int from a file. -
ReneG - 20.04.2013
you're going to have to find a way to do a strcmp check rather than a strfind
Re: Getting an int from a file. -
thefatshizms - 20.04.2013
If you look, I have 2 strcmp's to try and counter this problem.
Re: Getting an int from a file. -
Jefff - 21.04.2013
pawn Код:
stock INI_GetInt(INI:filehandle, Key[], &dest)
{
if(!filehandle)
{
printf("Failed to open file!");
return;
}
new
line[128],
len = strlen(Key);
while( fread(filehandle, line) )
{
if(!strcmp(line, Key, true, len) && line[len] == '=')
{
strmid(line,line,len+1,strlen(line)-2);
dest = strval(line);
break;
}
}
}
Re: Getting an int from a file. -
thefatshizms - 21.04.2013
Thanks