Snippet crashes my server :/
#1

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;
}
Reply
#2

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.
Reply
#3

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;
}
Reply
#4

Yeah you are right
Reply
#5

My bad.
Reply
#6

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);
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)