[COD] Guardar/Carregar strings/texto em ficheiros
#1

Epб eu comecei a utilizar as FileFunctions/Raw mas confesso que ainda me confundo. Entгo foi assim, como comecei a utilizar e lembrei-me que tanta gente perguntava a mesma coisa. Entгo decidi meter a cabecinha a trabalhar e tentei fazer. Epб foi uma dor de cabeзa, eu estava cheio de sono, nгo sabia a quantas andava e acabei por fazer asneiras. Vim cб ao fуrum e pedi ajuda na parte inglesa, na qual fui ajudado pelo Zeex que me resolveu logo o problema. Por mais estъpido que pareзa, eu estava a fechar o ficheiro dentro do loop, por isso nгo dava nada.

Isto estб simples e tambйm nгo estб scriptado da melhor forma, mas tentei deixar simples o suficiente para todos entenderem. Se nгo perceberem como funciona por exemplo o comando de ler, vгo aqui e tirem as vossas dъvidas todas.


Topo:

pawn Код:
#define azul        0x0000FFAA
#define branco      0xFFFFFFAA
#define vermelho    0xFF0000AA
#define verde       0x38FF06FF

OnPlayerCommandText:

pawn Код:
new cmd[128], idx;
    cmd = strtok(cmdtext, idx);
   
    if(strcmp(cmd, "/regadm", true) == 0)
    {
        new tmp[128], gajo, pname[24], string[30], string2[128];
        tmp = strtok(cmdtext, idx);
       
        gajo = strval(tmp);

        if(strlen(tmp) == 0) return SendClientMessage(playerid, 0xFFFFFFFF, "USO: /regadm [playerid]");

        if(!IsPlayerConnected(gajo))
        {
        return SendClientMessage(playerid, vermelho, "ID Invбlido!");
        }
       
        if(!IsNumeric(tmp))
        {
            return SendClientMessage(playerid, vermelho, "Apenas nъmeros/ids podem ser utilizados!");
        }
       
        new File:ftw=fopen("admins.txt", io_append);
        GetPlayerName(gajo,pname,24);
        format(string, 30, "%s\r\n", pname);
        fwrite(ftw, string);
        fclose(ftw);
        format(string2,128,"%s foi registado como admin com sucesso!",pname);
        SendClientMessage(playerid,verde,string2);
        return 1;
    }

    if(strcmp(cmd, "/leradm", true) == 0)
    {
        new string[512];
        new File:ftw=fopen("admins.txt",io_read);
        new l;
        while(fread(ftw,string))
        {
            l++;
            if(l==1)
            {
                SendClientMessage(playerid,azul,string);
            }
            else
            {
                SendClientMessage(playerid,branco,string);
            }
        }
        fclose(ftw);
        return 1;
    }

Fim:

pawn Код:
strtok(const string[], &index)
{
    new length = strlen(string);
    while ((index < length) && (string[index] <= &#39; '))
    {
        index++;
    }

    new offset = index;
    new result[20];
    while ((index < length) && (string[index] > &#39; ') && ((index - offset) < (sizeof(result) - 1)))
    {
        result[index - offset] = string[index];
        index++;
    }
    result[index - offset] = EOS;
    return result;
}

IsNumeric(const string[])
{
    for (new i = 0, j = strlen(string); i < j; i++)
    {
        if (string[i] > &#39;9' || string[i] < '0') return 0;
    }
    return 1;
}

Importante: Nos scriptfiles deverб existir um ficheiro com o nome 'admins.txt'. A primeira linha colocam por exemplo 'Admins:'. E й importantнssimo que o cursor esteja na linha abaixo da 1Є linha (Segunda linha).


Crйditos:
- SA-MP Team (Pelo modo multiplayer e todas as funзхes que foram disponibilizadas a nуs).
- Wiki (Pela Strtok, como usб-la, e pelo copy/past que fiz de lб para fazer os comandos e tudo mais).
- Criador do IsNumeric. (Nгo sei quem foi).
- Sub_Zero (Esteve juntamente comigo a perceber/resolver os erros).
- Zeex (Mostrou-me o erro que estava a fazer o comando de ler nгo funcionar).
- DraKoN (Por ser veado ).
- Eu (Por juntar esta treta toda).


Nota: Como jб disse acima isto estб simples e mal scriptado, por isso pode ser muito melhorado!

P.S - Desculpem se tiver algum erro, mas estou muito 'confuso' para verificar melhor. Caso surja eu ajeito ou alguйm ajeita.


Exemplo em filterscript + ficheiro .txt: http://solidfiles.com/d/59e/
Reply
#2

Vocк Poderia Colocar Admin ON e OFF
Usando Assim
GetPlayerName = ARQUIVO
Entгo
ON/OFF
Sacas?

P.S: Afff nгo, eu nao ajudei nisso
eu fiz o tutorial de como salvar usando isso O.o Lembra?
Reply
#3

Quote:
Originally Posted by DraKoN
rsrsrws
bjoos na boca amor
Йs gay porra? xD
Reply
#4

Saco. Se eu fizer um update posto aqui.

Eu usei a wiki para salvar nгo o teu tuto.
Reply
#5

Код:
strtok(const string[], &index)
{
new length = strlen(string);
while ((index < length) && (string[index] <= ' '))
{
index++;
}

new offset = index;
new result[20];
while ((index < length) && (string[index] > ' ') && ((index - offset) < (sizeof(result) - 1)))
{
result[index - offset] = string[index];
index++;
}
result[index - offset] = EOS;
return result;
}

IsNumeric(const string[])
{
for (new i = 0, j = strlen(string); i < j; i++)
{
if (string[i] > '9' || string[i] < '0') return 0;
}
return 1;
}
Onde ponho Isso
Reply
#6

Coloca ao Final do GM
Reply
#7

Reply
#8

Mas da isso
Код:
C:\DOCUME~1\winxp\Desktop\VIRTUA~1\_BRASI~1\GAMEMO~1\Virtual.pwn(12206) : warning 219: local variable "tmp" shadows a variable at a preceding level
C:\DOCUME~1\winxp\Desktop\VIRTUA~1\_BRASI~1\GAMEMO~1\Virtual.pwn(12206) : warning 219: local variable "string" shadows a variable at a preceding level
C:\DOCUME~1\winxp\Desktop\VIRTUA~1\_BRASI~1\GAMEMO~1\Virtual.pwn(12235) : warning 219: local variable "string" shadows a variable at a preceding level
C:\DOCUME~1\winxp\Desktop\VIRTUA~1\_BRASI~1\GAMEMO~1\Virtual.pwn(43920) : error 021: symbol already defined: "strtok"
C:\DOCUME~1\winxp\Desktop\VIRTUA~1\_BRASI~1\GAMEMO~1\Virtual.pwn(43939) : error 021: symbol already defined: "IsNumeric"
Pawn compiler 3.2.3664	 	 	Copyright © 1997-2006, ITB CompuPhase


2 Errors.
Reply
#9

Deleta Linhas:
12206
12235

Agora:
Apaga Aquele Cуdigo do Final que tu acabou de Colocar
strtok e isnumeric
Reply
#10

Da mais erro. ¬¬
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)