[stock] Remove Double Lines From File
#1

Here's a useful stock I made for mt gamemode, I thought it might be useful to others too so here it is:

stock RemoveDoubleLines(from[],to[]="",bool:ignorecase=false)
{
if(strlen(to)<1) format(to,strlen(from)+1,"%s",from);
new string[300],File:file=fopen(from,io_read),File:copy=fopen("tm pfile.tmp",io_write);
while(fread(file,string)) fwrite(copy,string); fclose(file); fclose(copy);
new str2[sizeof string],count=0,count2=0,bool:iscopy[1500]=false; file=fopen(from,io_read);
while(fread(file,string))
{
copy=fopen("tmpfile.tmp",io_read); count2=0;
while(fread(copy,str2)&&count2<count)
{
if(!strcmp(string,str2,ignorecase)) iscopy[count]=true; count2++;
}
fclose(copy); count++;
}
fclose(file); file=fopen("tmpfile.tmp",io_read); copy=fopen(to,io_write);
while(fread(file,string)) if(!iscopy[count]) fwrite(copy,string);
fclose(file); fclose(copy); fremove("tmpfile.tmp");
}

====What is it====
This will check if a file cotains double lines (2 lines or more containing the same text), and will remove the lines that are repeated (if a line is repeated it will appear once). The destination file can be the same or another.

===Parameters====(or arguments)
from : the name of the source file to be read
to (optional): the name of the destination file th store the text with no double lines in
If not set, the edits will be made directly to the first file
ignorecase (optional) : true : the double lines check ignores case
false : the double lines check is case-sensitive
default (when not set) : false

===How to use===
I know this example is not useful and is very bad but I wanted to make it easy to understand:

public OnPlayerUpdate(playerid)
{
new Float:HP; GetPlayerHealth(playerid,HP);
new File:file=fopen("Health.txt",io_append);
new string[40]; format(string,40,"Someone's health has changed to %f",HP);
fwrite(file,string); fclose(file);
}

public OnGameModeExit()
{
RemoveDoubleLines("Health.txt");
new lines=0; new string[40];
new File:file=fopen("Health.txt",io_read);
while(fread(file,string)) lines++; fclose(file);
printf("The average amount of HP lost is %d",100/lines);
}

Before the gamemode ends an example of values in file is :
100
100
100
76
76
74
74
0
0
0
After stock is called :
100
76
74
0

===Order===
The order gives priority to the first lines: if a line hasn't been repeated before, it will be copied.
If you want to give priority to the last lines look for 'count2<count' on the stock and change it to >

===How does it work=== 3 steps:

1st step:
Copy the file into another names "tmpfile.tmp"

2nd step:
Each line read from the file will be compared with the previous lines read. For this we need the 2 files that are the same. If a line was found the same then the line from the first loop is a copy. At the end of step 2 each line number will be assigned to a boolean value that indicates if its a copy or not

3rd step:
Lines that are not copies will be written to the destination file. They will be copied from "tmpfile.tmp" to prevent the bug if the destination is the same as the source

That's it
Reply
#2

Post this here, and place it between [pawn] tags.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)