Question about Fopen/fread
#1

Hello there,
I am stuck at something.
I managed to save /report succesfully.
But how to make it load?
And more important, how to make it only show latest 10 reports?

code
Код:
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\".");
        	}
Could someone provide me with a small example on how to load the latest 10 reports from that file?

I know about https://sampwiki.blast.hk/wiki/Fread
But i just don't get how to show them:

Код:
if(handle)
{
	// Success
 
	// Read the whole file
	while(fread(handle, buf)) print(buf);
 
	// Close the file
	fclose(handle);
}
Thanks in advance
Reply
#2

I did this but it does not show any text.

PHP код:
[CODE]CMD:lastreports(playerid,params[])
{
    new 
str[1024],string2[1024],buf[1024];
    if(
AdminLevel[playerid] >=2)
    {
        for(new 
iMAX_REPORTS; ++i)
        {
            new 
File:handle fopen("Logs/playerreports.txt"io_read);
            while(
fread(handlestr))
            if(
handle)
            {
                
format(str,sizeof(str),"%s\n\n",buf);
                
strcat(string2,str);
                
ShowPlayerDialog(playeridDIALOG_NONEDIALOG_STYLE_MSGBOX"Latest reports"string2"OK""");
                
fclose(handle);
            }
            else
            {
                print(
"Failed to open file \"playerreports.txt\".");
            }
        }
    }
    return 
1;
}[/
CODE
What am i doing wrong?
Reply
#3

So i have this now, but it shows like really messed up.
These are the reports in the file:
[14/12/2017][14:14:39]Jasper_Corleone has been reported by Jasper_Corleone. Reason: '1 testreport'

[14/12/2017][14:14:44]Jasper_Corleone has been reported by Jasper_Corleone. Reason: '1 testreport2'

[14/12/2017][14:16:12]Jasper_Corleone has been reported by Jasper_Corleone. Reason: '1 testreport3'

[14/12/2017][14:16:41]Jasper_Corleone has been reported by Jasper_Corleone. Reason: '1 testreport4'


It shows this:


How to fix this?

PHP код:
CMD:lastreports(playerid,params[])
{
    new 
str[1024],string2[1024],buf[128];
    if(
AdminLevel[playerid] >=2)
    {
        for(new 
iMAX_REPORTS; ++i)
        {
            new 
File:handle fopen("Logs/playerreports.txt"io_read);
            if(
handle)
            {
                while(
fread(handlebuf))
                {
                    
format(strsizeof(str), "%s%s\n"strbuf);
                    
strins(string2strstrlen(str));
                    
ShowPlayerDialog(playerid0DIALOG_STYLE_MSGBOX"Last reports"string2"OK""");
                    
fclose(handle);
                }
            }
            else
            {
                print(
"Failed to open file \"playerreports.txt\".");
            }
        }
    }
    return 
1;

Reply
#4

Better to go with SQLite or MySQL because most people use it for saving logs too and its much more easier to work with.
Reply
#5

Quote:
Originally Posted by Logic_
Посмотреть сообщение
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?
Reply
#6

Quote:
Originally Posted by jasperschellekens
Посмотреть сообщение
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.
Reply
#7

Quote:
Originally Posted by Logic_
Посмотреть сообщение
Uh, just use that then :P The readability and efficiency of these file natives is not really worth spending and asking for help for.
Why even bother responding then?
Not everyone uses mysql, and most certainly, not everyone is gonna use mysql
Reply
#8

Use SQLite then, doesn't require a separate online database, saves database in .db format in scriptfiles folder, gives almost all the features of MySQL. However, Are you using file saving system for your account (register/ login system) script?
Reply
#9

You'd have to read the file twice though. Few examples:
pawn Код:
// 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);
}
pawn Код:
// 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);
}
and set log format to:
pawn Код:
"[%d/%d/%d][%d:%d:%d]%s reported %s (Reason: %s)\r\n"
With SQL as above users suggested, you'd also have a nice dialog table since each data would be on their own and the size of local arrays would be even smaller.
Reply
#10

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
You'd have to read the file twice though. Few examples:
pawn Код:
// 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);
}
pawn Код:
// 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);
}
and set log format to:
pawn Код:
"[%d/%d/%d][%d:%d:%d]%s reported %s (Reason: %s)\r\n"
With SQL as above users suggested, you'd also have a nice dialog table since each data would be on their own and the size of local arrays would be even smaller.
Thank you, worked.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)