File function problem - 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: File function problem (
/showthread.php?tid=399930)
File function problem -
liinor - 16.12.2012
It does remove text from line, but it doesn't input text to file.
Please help me to make it write.
//Up in my script
pawn Код:
#define FILE_NAME "/GunGameHighScores.txt" // The name of the file
new File:gungamefile;
pawn Код:
public fcreate(filename[])
{
if (fexist(filename)){return false;}
new File:fhandle = fopen(filename,io_write);
fclose(fhandle);
return true;
}
pawn Код:
fdeleteline(filename[], line[]){
if(fexist(filename)){
new temp[256];
new File:fhandle = fopen(filename,io_read);
fread(fhandle,temp,sizeof(temp),false);
if(strfind(temp,line,true)==-1){return 0;}
else{
fclose(fhandle);
fremove(filename);
for(new i=0;i<strlen(temp);i++){
new templine[256];
strmid(templine,temp,i,i+strlen(line));
if(equal(templine,line,true)){
strdel(temp,i,i+strlen(line));
fcreate(filename);
fhandle = fopen(filename,io_write);
fwrite(fhandle,temp);
fclose(fhandle);
return 1;
}
}
}
}
return 0;
}
//================================================== ======
//Command which suppose write data
pawn Код:
if (!strcmp("/piss", cmdtext, true)|| !strcmp("/pee", cmdtext, true))
{
SaveGunGameScore(playerid);
return 1;
}
//================================================== ======================
//saving line
pawn Код:
stock SaveGunGameScore(playerid) {
new string[256];
new pname[MAX_PLAYER_NAME];
gungamefile=fopen(FILE_NAME, io_append);
GetPlayerName(playerid, pname, 24); // The name of the player
format(gungamefilestr, sizeof(gungamefilestr), "%s %d\r\n",pname, GunGameScore[playerid]); // format
if(!gungamefile) fcreate(FILE_NAME); // Our check
while(fread(gungamefile, string)) {
if(strcmp(string, pname, false, strlen(pname))==0) { // To check if the players name is in the file
fdeleteline(FILE_NAME, string); // delete the line
fwrite(gungamefile, gungamefilestr); // write the string
}
else if(strcmp(string, pname, false, strlen(pname))!=0) { // Its NOT true
fwrite(gungamefile, gungamefilestr);
}
}
fclose(gungamefile);
}