SA-MP Forums Archive
Snippet crashes my server :/ - 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: Snippet crashes my server :/ (/showthread.php?tid=378438)



Snippet crashes my server :/ - BlackAtom - 17.09.2012

Hi! My code below crashes my server, why Please answer me, I'm waiting for your response )
pawn Код:
#include <a_samp>

public OnPlayerConnect(playerid)
{
new File:playerinfos, pntFinal[256], pntName[MAX_PLAYER_NAME+1], pntIp[16];
GetPlayerName(playerid,pntName,sizeof(pntName));
GetPlayerIp(playerid,pntIp,sizeof(pntIp));
format(pntFinal,sizeof(pntFinal),"Name: %s | ",pntName);
format(pntFinal,sizeof(pntFinal),"%sIP: %s | ",pntFinal,pntIp);
playerinfos = fopen("playerinfos.txt",io_write);
fwrite(playerinfos,pntFinal);
fclose(playerinfos);
return 1;
}



Re: Snippet crashes my server :/ - Glint - 17.09.2012

You are using "format" twice which is actually useless because you are formatting the same thing then erasing the old and re-formatting it again.

So just format then append your changes.

Then format again.


Re: Snippet crashes my server :/ - hittt - 17.09.2012

pawn Код:
#include <a_samp>

public OnPlayerConnect(playerid)
{
new File:playerinfos, pntFinal[MAX_PLAYER_NAME+15+11], pntName[MAX_PLAYER_NAME], pntIp[15];
GetPlayerName(playerid,pntName,sizeof(pntName));
GetPlayerIp(playerid,pntIp,sizeof(pntIp));
format(pntFinal,sizeof(pntFinal),"Name: %s | IP: %s",pntName,pntIp);
playerinfos = fopen("playerinfos.txt",io_append);
if (playerinfos)
{
fwrite(playerinfos,pntFinal);
fclose(playerinfos);
}
return 1;
}



Re: Snippet crashes my server :/ - BlackAtom - 17.09.2012

Yeah you are right


Re: Snippet crashes my server :/ - AtItsMax - 17.09.2012

My bad.


Re: Snippet crashes my server :/ - AndreT - 17.09.2012

You could always add line-by-line debugging to see where things go sour and it crashes, for example:
pawn Код:
print("fopen");
playerinfos = fopen(...);
print("fwrite");
fwrite(...);
print("fclose");
fclose(...);
My best guess is that you are not checking if the file pointer is valid (the file may not exist in the scriptfiles folder or sa-mp server executable might not have access to it, etc). You can check this by doing:
pawn Код:
playerinfos = fopen("playerinfos.txt", io_write);
if(playerinfos) // Check if the file handle is valid!
{
    fwrite(playerinfos, pntFinal);
    fclose(playerinfos);
}
Also, read the documentation for these natives: https://sampwiki.blast.hk/wiki/Fopen https://sampwiki.blast.hk/wiki/Fclose https://sampwiki.blast.hk/wiki/Fwrite
The wiki might have some information about common pitfalls.

Quote:
Originally Posted by Lexi'
Посмотреть сообщение
You are using "format" twice which is actually useless because you are formatting the same thing then erasing the old and re-formatting it again.

So just format then append your changes.

Then format again.
pawn Код:
format(string, sizeof(string), "Hey, %s!", szSomething);
format(string, sizeof(string), "%s|Oh hello there!", string);
... this is perfectly valid code. But is it beneficial? In most cases, no.

In the current case, I see he does not do much, in fact:
pawn Код:
format(pntFinal,sizeof(pntFinal),"Name: %s | ",pntName);
format(pntFinal,sizeof(pntFinal),"%sIP: %s | ",pntFinal,pntIp);
Could easily become:
pawn Код:
format(pntFinal, sizeof(pntFinal), "Name: %s | IP: %s | ", pntName, pntIp);