Data is not OverWriting
#1

PHP код:
CMD:oban(playerid,params[])
{
    new 
targetname[24];
    new 
Name[MAX_PLAYER_NAME];
    new 
Msg[128];
    new 
file[100], File:PFilestr1[100] , str2[100];
    if (
APlayerData[playerid][LoggedIn] == true)
    {
        
// Check if the player's admin-level is at least 3
        
if (APlayerData[playerid][PlayerLevel] >= 3)
        {
    if(
sscanf(params"s[24]"targetname)) return SendClientMessage(playerid, -1"Correct Usage: /oban [Player's Name]");
    else
            {
                
// Get the name of the admin
                
GetPlayerName(playeridNamesizeof(Name));
                
// Construct the complete filename for this player's account
                
format(filesizeof(file), PlayerFiletargetname);
                
// Check if the file exists
                
if (fexist(file))
                {
                    
PFile fopen(fileio_append); // Open the playerfile for appending (this command only appends a new line to overwrite the bantime)
                    
                    
format(str1100"BanTime 2147483640\r\n");// Construct the line: "BanTime <0>"
                    
format(str2,100,"Bans 5\r\n");
                    
fwrite(PFilestr2);
                    
fwrite(PFilestr1); // And save it to the file
                    
fclose(PFile); // Close the file
                
                    
format(Msg128"%s %s has offline-banned %s"AdminLevelName[APlayerData[playerid][PlayerLevel]], Nametargetname);
                    
SendClientMessageToAll(0x808080FFMsg);
                }
                else
                    
SendClientMessage(playerid0xFFFFFFFF"{FF0000}There is no account with that playername");
            }
        }
    }
    return 
1;

This is the CMD I made . It doesn't give me erros but when I try it Instead of replacing the Variables in the player files it makes one more for example :-
PHP код:
Data1
Data2
Ban
BanTime
Data5
Data6
Ban
//This gets created again instead of overwriting the above vars 
Bantime// 
How do I fix this ?
Reply
#2

https://sampwiki.blast.hk/wiki/File_Functions

What you have to do is first delete the line and re append the data you need.


Check out the wiki.
Reply
#3

You used io_append, that will only append the data and not overwrite it
You need to copy the complete file into memory or a tmp file, see ftemp() (it is opened in io_readwrite)
After you copied everything into the tmp file, you read line for line and copy the data back into the main file
If it hits "Ban" or "BanTime" than you add your new lines instead of the old

Edit: you can also do it like darklord said than you don't need a tmp file
Reply
#4

Please Give me Example can't really understand Plus How do I delete the 2 lines of Ban / Bantime ?
Reply
#5

Anyone please ? I need it urgently ?
Reply
#6

If you don't understand it then don't try to write your own user system. There are a number of good file function includes, the most famous one probably being Y_Ini.
Reply
#7

IK I used Y_INI Before I just want this .. cuz the script that I am using uses its own system so please can you tell it to me ?
Reply
#8

We could provide something like that but you could also use ******

http://lmgtfy.com/?q=freplaceline+si...orum.sa-mp.com
Reply
#9

Okay After Googling I found a stock by FireCat And I am using it Stock is
PHP код:
fRemoveLine(file[],line[])//By: Firecat
{
    new 
string[256],
        
File:Temp fopen("Temp.ini",io_append),
        
File:Main fopen(file,io_read);

    while(
fread(Main,string))
    {
        if(
strcmp(string,line) != 0)
        {
            
fwrite(Temp,string);
        }
    }
    
fclose(Main);
    
fclose(Temp);
    
fremove(file);
    
    
Main fopen(fileio_append);
    
Temp fopen("Temp.ini",io_read);
    
    while(
fread(Temp,string))
    {
        
fwrite(Main,string);
    }
    
fclose(Main);
    
fclose(Temp);
    
fremove("Temp.ini");
    return 
1;

But when I use it in the following way
PHP код:
fRemoveLine(PFile,"Bans");
                    
fRemoveLine(PFile,"BanTime"); 
I get the following error
PHP код:
error 035argument type mismatch (argument 1)
error 035argument type mismatch (argument 1
Reply
#10

The first parameter is the filename not the handle

Also I am not sure if strcmp will find only the part of the string, if it doesn't work edit it to that

pawn Код:
fRemoveLine(file[],line[])//By: Firecat
{
    new string[256],
        len = strlen(line), // added
        File:Temp = fopen("Temp.ini",io_append),
        File:Main = fopen(file,io_read);

    while(fread(Main,string))
    {
        if(strcmp(string,line,false,len) != 0) // changed
        {
            fwrite(Temp,string);
        }
    }
    // rest stays the same
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)