Remove an line from .txt file
#1

Hi guys

I have .txt file with skins,for example:

Код:
0
1
2
3
4
10
15
20
50
128
I want to make that I can add/delete skins IG!Adding skin is simple,but problem is remove line.

I'm try with this function:
Код:
fdeleteline(filename[], removed[])
{
	new string[64], str[32], File:handle, File:ftmp;
	handle = fopen(filename,io_read);
	format(str,sizeof(str),"%s.part",filename);
	ftmp = fopen(str,io_write);
	while(fread(handle,string))
		if(strfind(string,removed) == -1)
			fwrite(ftmp,string);
	fclose(handle);
	fclose(ftmp);
	handle = fopen(filename,io_write);
	ftmp = fopen(str,io_read);
	while(fread(ftmp,string))
		fwrite(handle,string);
	fclose(handle);
	fclose(ftmp);
	return fremove(str);
}
Problem is when I use this:
fdeleteline("skins.txt","0");
So I want that he delete 0 skin,he will delete 10,20,50 ...I don't know how to fix this..
Reply
#2

Bump :/
Reply
#3

Код:
while(fread(handle,string))
		if(strfind(string,removed) == -1) {
fwrite(ftmp,string);
break;
}
Use that or change it to strcmp and compare the entire length of the string.
Reply
#4

PHP код:
fdeleteline(filename[], removed[])
{
    new 
string[64], str[32], File:handleFile:ftmp;
    
handle fopen(filename,io_read);
    
format(str,sizeof(str),"%s.part",filename);
    
ftmp fopen(str,io_write);
    while(
fread(handle,string))
        if(
strcmp(string,removedtrue))
            
fwrite(ftmp,string);
    
fclose(handle);
    
fclose(ftmp);
    
handle fopen(filename,io_write);
    
ftmp fopen(str,io_read);
    while(
fread(ftmp,string))
        
fwrite(handle,string);
    
fclose(handle);
    
fclose(ftmp);
    return 
fremove(str);

Reply
#5

It don't work...(No one..)
Reply
#6

Just use SQLite.

PHP код:
#define        SQL_DATABASE_NAME        "settings.db"
#define        TABLE_SKINS                "`Skins`"
new DBsqlDatabase;
public 
OnGameModeInit() {
    
    
sqlDatabase db_open(SQL_DATABASE_NAME);
    if (
sqlDatabase == DB0) {
        print(
"An error occured trying to create a database.");
        return 
1;
    }
    
db_free_result(db_query(sqlDatabase"CREATE TABLE IF NOT EXISTS "#TABLE_SKINS" (`SkinID` INTEGER)"));
    
    
return 1;
}
public 
OnGameModeExit() {
    
    if (
sqlDatabase != DB0) {
        
db_close(sqlDatabase);
    }
    
    return 
1;
}
RemoveSkin(skinid) {
    new 
query[54];
    
format(querysizeof query"DELETE FROM "#TABLE_SKINS" WHERE `SkinID` = '%d'", skinid);
    
db_free_result(db_query(sqlDatabasequery));
    return 
1;
}
InsertSkins(skinid) {
    new 
query[54];
    
format(querysizeof query"INSERT INTO "#TABLE_SKINS" (`SkinID`) VALUES('%d')", skinid);
    
db_free_result(db_query(sqlDatabasequery));
    return 
1;

Reply
#7

or change
pawn Код:
if(strfind(string,removed) == -1)
to

pawn Код:
if(strfind(string,removed) != 0)

or

if(strfind(string,removed))
Reply
#8

Quote:
Originally Posted by Jefff
Посмотреть сообщение
or change
pawn Код:
if(strfind(string,removed) == -1)
to

pawn Код:
if(strfind(string,removed) != 0)

or

if(strfind(string,removed))
Remvoing skin id 5
Id 5 gets removed
Id 50 - 59 gets removed
Remove skin id 1
Id 1 gets removed id 10 - 19 gets removed...
Remove skin id 2
Id 2 gets removed id 20 - 29 gets removed

You've disappointed me Jefff, you have always been my inspiration <3
Reply
#9

Original poster:
strfind is used to find a specific substring in a string. Lets say you have this string:

"I am a duck"-

if you use strfind, the function will search through the string until it finds what you are looking for.
lets try to find "am"?
strfind returns 2. (because "am" was found on the 3rd spot in the string.)


You should definitly use strcmp (read it as stringcompare). This function compares the original string with the string you want to find, and it has to be exactly the same.
"am" is not equal to "I am a duck".
Same goes to your problem. If you want to find "0" or "13" - you need to compare the entire word.
strfind searching for "1" will locate every number with 1 in it in your case - and delete them.
strcmp will not, as it will only take care of the "exact" same.

Available documentation on the functions here:
https://sampwiki.blast.hk/wiki/Strfind
https://sampwiki.blast.hk/wiki/Strcmp

You should get familiar with it's differences and use them correctly.
Reply
#10

denNorske:
jlalt is used strcmp and it don't work ( http://forum.sa-mp.com/showpost.php?...16&postcount=4 )

Logic:
Yeah.Thank you for your time to make this.I use MySQL and i know make this in MySQL.But I use include mSelection ( https://sampforum.blast.hk/showthread.php?tid=407045 ) and he load skins/vehicles from .txt file.So I want use it if it's possible.If not,i will use MySQL.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)