SA-MP Forums Archive
How to read from a list from a text file? - 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: How to read from a list from a text file? (/showthread.php?tid=576146)



How to read from a list from a text file? - Aerotactics - 01.06.2015

I understand kinda how file reading works, but I can't get my script to work.

If someone joins the server with a listed IP, they will be opped, simple as that. I dont know how to read for each IP, etc.

pawn Код:
public OnPlayerConnect(playerid)
{
    GameTextForPlayer(playerid,"~r~Destruction Derby",5000,5);
    RemoveBuildingForPlayer(playerid, 11394, -2048.1641, 168.3125, 31.7344, 0.25); //Class Select
    IsPlayerJoined[playerid] = false;
   
    new File:oplist = fopen("oplist.txt");
    if(oplist)
    {
        new str[128];
        while(fread(oplist, str))
        {
            new playerIP[16];
            GetPlayerIp(playerid, playerIP, 16);
            if(strcmp(playerIP, str) == 0)
            {
                IsOp[playerid] = true;
                SendClientMessage(playerid, -1, "Use /ophelp to see the op guide.");
            }
        }
        fclose(oplist);
    }
    else
    {
        print("The file \"oplist.txt\" does not exists, or can't be opened.");
    }
    return 1;
}



AW: How to read from a list from a text file? - Kaliber - 01.06.2015

Write this fix:

Код:
new str[128],playerIP[16];
while(fread(oplist, str))
{
    GetPlayerIp(playerid, playerIP, 16);
    switch(str[strlen(str)-2]) {
        case 13: str[strlen(str)-2] = EOS;
    }
    if(strcmp(playerIP, str) == 0) {
        IsOp[playerid] = true;
	SendClientMessage(playerid, -1, "Use /ophelp to see the op guide.");
    }
}
Greekz


Re: How to read from a list from a text file? - Konstantinos - 01.06.2015

If in every line an IP is stored without anything else such as tags etc, the code about reading and comparing is correct though I'd still not recommend file functions for such things. SQL is far better.

Also few tips: get the player's IP only once (outside of the loop) and if the IPs do match, stop the loop as there isn't any reason to keep reading from the file.


Re: AW: How to read from a list from a text file? - Aerotactics - 01.06.2015

Quote:
Originally Posted by Kaliber
Посмотреть сообщение
Write this fix:

Код:
new str[128],playerIP[16];
while(fread(oplist, str))
{
    GetPlayerIp(playerid, playerIP, 16);
    switch(str[strlen(str)-2]) {
        case 13: str[strlen(str)-2] = EOS;
    }
    if(strcmp(playerIP, str) == 0) {
        IsOp[playerid] = true;
	SendClientMessage(playerid, -1, "Use /ophelp to see the op guide.");
    }
}
Greekz
Doesn't seem to work.

And @Konstantinos I planned on closing the loop once I got this part working.


AW: Re: AW: How to read from a list from a text file? - Kaliber - 01.06.2015

Quote:
Originally Posted by Aerotactics
Посмотреть сообщение
Doesn't seem to work.
It should...

Well, make a print-Debug, show me what is printed...

check the ips, for me it's working fine...


Re: How to read from a list from a text file? - Konstantinos - 01.06.2015

Can you show an example of how it's saved in oplist.txt?


Re: AW: Re: AW: How to read from a list from a text file? - Aerotactics - 01.06.2015

Quote:
Originally Posted by Kaliber
Посмотреть сообщение
It should...

Well, make a print-Debug, show me what is printed...

check the ips, for me it's working fine...
Yeah, the debug shows it is reading correctly, but it's not familiarizing with my ip address.

list Example:

123.456.789.999
123.45.67.890
1.2.34.56

EDIT: Would it recognize my IP even if I'm connected via "localhost" or would it not be able to read my IP (it's own IP)? XD


AW: Re: AW: Re: AW: How to read from a list from a text file? - Kaliber - 01.06.2015

Quote:
Originally Posted by Aerotactics
Посмотреть сообщение
EDIT: Would it recognize my IP even if I'm connected via "localhost" or would it not be able to read my IP (it's own IP)? XD
If you connect through localhost it reads: 127.0.0.1