new File:log = fopen("Logs/playerreports.txt", io_append); if(log) { getdate(Year, Month, Day); gettime(hours, minutes, seconds); format(string,sizeof(string),"[%d/%d/%d][%d:%d:%d]%s reported %s (Reason: %s)'\n\n",Day,Month,Year,hours,minutes,seconds,PlayerName(ID),PlayerName(playerid),reason); fwrite(log, string); fclose(log); } else { print("Failed to open file \"playerreports.txt\"."); }
if(handle) { // Success // Read the whole file while(fread(handle, buf)) print(buf); // Close the file fclose(handle); }
[CODE]CMD:lastreports(playerid,params[])
{
new str[1024],string2[1024],buf[1024];
if(AdminLevel[playerid] >=2)
{
for(new i; i < MAX_REPORTS; ++i)
{
new File:handle = fopen("Logs/playerreports.txt", io_read);
while(fread(handle, str))
if(handle)
{
format(str,sizeof(str),"%s\n\n",buf);
strcat(string2,str);
ShowPlayerDialog(playerid, DIALOG_NONE, DIALOG_STYLE_MSGBOX, "Latest reports", string2, "OK", "");
fclose(handle);
}
else
{
print("Failed to open file \"playerreports.txt\".");
}
}
}
return 1;
}[/CODE]
CMD:lastreports(playerid,params[])
{
new str[1024],string2[1024],buf[128];
if(AdminLevel[playerid] >=2)
{
for(new i; i < MAX_REPORTS; ++i)
{
new File:handle = fopen("Logs/playerreports.txt", io_read);
if(handle)
{
while(fread(handle, buf))
{
format(str, sizeof(str), "%s%s\n", str, buf);
strins(string2, str, strlen(str));
ShowPlayerDialog(playerid, 0, DIALOG_STYLE_MSGBOX, "Last reports", string2, "OK", "");
fclose(handle);
}
}
else
{
print("Failed to open file \"playerreports.txt\".");
}
}
}
return 1;
}
Better to go with SQLite or MySQL because most people use it for saving logs too and its much more easier to work with.
|
mysql is a completely different coding language.
I know it is possible on this way. Anyone who knows how to fix it? |
Uh, just use that then :P The readability and efficiency of these file natives is not really worth spending and asking for help for.
|
// Reading 10 last reports in ascending order:
new File: fhandle = fopen("Logs/playerreports.txt", io_read), buf[256];
if (fhandle)
{
new lines, at, index, offset;
while (fread(fhandle, buf)) lines++;
at = lines - 10;
lines = 0;
fseek(fhandle);
while ((offset = fread(fhandle, buf)))
{
if (++lines <= at) continue;
if (buf[offset - 2] == '\r') buf[offset - 2] = EOS;
else if (buf[offset - 1] == '\n') buf[offset - 2] = EOS;
printf("%i. \"%s\"", ++index, buf);
}
fclose(fhandle);
}
// Reading 10 last reports in descending order:
new File: fhandle = fopen("Logs/playerreports.txt", io_read), buf[256];
if (fhandle)
{
new lines, at, index = -1, offset, buf_reports[10][256];
while (fread(fhandle, buf)) lines++;
at = lines - 10;
lines = 0;
fseek(fhandle);
while ((offset = fread(fhandle, buf)))
{
if (++lines <= at) continue;
strcat(buf_reports[++index], buf);
if (buf_reports[index][offset - 2] == '\r') buf_reports[index][offset - 2] = EOS;
else if (buf_reports[index][offset - 1] == '\n') buf_reports[index][offset - 2] = EOS;
}
for (new i = 9; i > -1; i--)
{
printf("%i. \"%s\"", 10 - i, buf_reports[i]);
}
fclose(fhandle);
}
"[%d/%d/%d][%d:%d:%d]%s reported %s (Reason: %s)\r\n"
You'd have to read the file twice though. Few examples:
pawn Код:
pawn Код:
pawn Код:
|