How to delete a line in a .txt file?
#1

Good day SAMP Community,

I have the following Problem: I wanted to script a /foff Script where Offline Players are shown. Since I do not have Mysql, I created for all Factions a log File. In this Log File the Player Names are saved when they are being invited. But, when they are getting uninvited they need to be removed from this File and this is my problem. I do not know how to delete a Name from a textfile. For example I /uninvite Name2, then Name 2 should be deleted from the list. If I /uninvite Firstname_Lastname then Firstname_Lastname should be deleted
The names are saved like this
Код:
Name
Name1
Name2//this should be removed
Name3
Name4
Firstname_Lastname
Lastname_Firstname
etc
I tried it but it doesnt work. I never get the message Test, only fail and the line wont be deleted
pawn Код:
COMMAND:auninvite(playerid, params[])
{
    if(GetAdminLevel(playerid) < 10) return SendClientError(playerid, CANT_USE_CMD);
    new iPlayer;
    if( sscanf ( params, "u", iPlayer)) return SCP(playerid, "[PlayerID/PartOfName]");
    if(!IsPlayerConnected(iPlayer)) return SendClientError(playerid, PLAYER_NOT_FOUND);
// The function itself
            new File:gstats; // The file
            new string[MAX_STRING];
            new pname[24];
            new str[256];
            new dest[128];
            format(dest,sizeof(dest), "fLogs/%s.fam.log",PlayerInfo[iPlayer][PTeamName]);
            gstats=fopen(dest, io_append);
            GetPlayerName(iPlayer, pname, 24); // The name of the player
             format(str, 256, "\n\r"); // format
             while(fread(gstats, string)) {
             if(strcmp(string, pname, false, strlen(pname))==0) { // To check if the players name is in the file
             fdeleteline(dest, string); // delete the line
             fwrite(gstats, str); // write the string
             SendClientWarning(playerid,"Test");//i never see this message only "Fail"
                   }
               }
   
                 fclose(gstats);
         SendClientWarning(playerid,"Fail");//this is the message i only see:(
    Uninvite(iPlayer,GetPlayerFaction(iPlayer));
    SendClientInfo(playerid, "Done.");
    return 1;
}
Kind regards,

Lionel
Reply
#2

Please help!
Reply
#3

fdeleteline automaticly opens, reads, deletes and closes. You'll have to open the file again to write to it.

Edit: IIRC.
Reply
#4

I dont want to write to it, I want just to delete the line. But it doesnt work. I dont get the Message "Test" only "fail"
Reply
#5

Let us see your fdeleteline function
Reply
#6

Since I haven't tested the code, I'm not sure if it will work. However try this:

pawn Код:
CheckName(teamname[], name[])
{
    new string[20];
    new destination[128];
    format(destination, sizeof(destination), "fLogs/%s.fam.log", teamname);
    new File: file = fopen(destination, io_read); // I'm not sure what's the name of the log file.
    while(fread(file, string))
    {
        if (strcmp(name, string, true, strlen(name)) == 0)
        {
            fclose(file);
            return 1;
        }
    }
    fclose(file);
    return 0;
}

RemoveName(teamname[], name[])
{
    if(CheckName(teamname, name) == 1)
    {
        new string[20];
        new destination[128];
        format(destination, sizeof(destination), "fLogs/%s.fam.log", teamname);
        new File: file = fopen(destination, io_read);
        fcreate("TempFamLog.cfg");
        new File: file2 = fopen("TempFamLog.cfg", io_append);
        while(fread(file, string))
        {
            if (strcmp(name, string, true, strlen(name)) != 0 && strcmp("\n", string) != 0)
            {
                fwrite(file2, string);
            }
        }
        fclose(file);
        fclose(file2);
        file = fopen(destination, io_write);
        file2 = fopen("TempFamLog.cfg", io_read);
        while(fread(file2, string))
        {
            fwrite(file, string);
        }
        fclose(file);
        fclose(file2);
        fremove("TempFamLog.cfg");
        return 1;
    }
    return 0;
}

COMMAND:auninvite(playerid, params[])
{
    if(GetAdminLevel(playerid) < 10) return SendClientError(playerid, CANT_USE_CMD);
    new iPlayer;
    if( sscanf ( params, "u", iPlayer)) return SCP(playerid, "[PlayerID/PartOfName]");
    if(!IsPlayerConnected(iPlayer)) return SendClientError(playerid, PLAYER_NOT_FOUND);
   
    new pname[24];
    new dest[128];
    GetPlayerName(iPlayer, pname, sizeof(pname));
    RemoveName(PlayerInfo[iPlayer][PTeamName], pname);
    Uninvite(iPlayer,GetPlayerFaction(iPlayer));
    return 1;
}
Reply
#7

pawn Код:
fdeleteline(filename[], line[])
{
    if(fexist(filename))
    {
        new temp[128];
        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[128];
                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;
}
Reply
#8

ok I am testing your function. Still doesnt work :/
Reply
#9

Can you post any warnings or errors?
Reply
#10

i needed to rename teamname to team than i didnt get any errors. But it doesnt delete the line
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)