Reading last 10 lines in a .txt file
#1

Hello,

I have a log file named Player_Reports.txt.Now I want to show last 10 reports of players when admins type /reports
Can someone give me a stock or a function that gives last 10 lines of a log file

last 10 lines means 10 latest reports

THANKS
Reply
#2

Propably you need read whole file and count lines then open again and reads only last 10 lines xd
Reply
#3

Quote:
Originally Posted by Jefff
Посмотреть сообщение
Propably you need read whole file and count lines then open again and reads only last 10 lines xd
Store the positions of the last ten lines within a file, then use fseek to jump to the first of those ten lines and start reading.
Reply
#4

Why not use MySQL? Easiest way ever.
Reply
#5

Not the easiest but the most effective way, SQL is a whole language apart tho
Reply
#6

Thanks but can someone give me a stock or a function like

ReadLastLines(const LogFile[])
Reply
#7

Search over internet for general logic of file buffer. You then just have to apply that logic in PAWN. It will be a good exercise for you.
Reply
#8

GTLS, I found nothing by searching like that

Why everyone not giving what i need

I need a function
Reply
#9

Quote:
Originally Posted by GameOvr
Посмотреть сообщение
GTLS, I found nothing by searching like that

Why everyone not giving what i need

I need a function
Why everyone not giving me a Million dollars.

I need money.
Reply
#10

Quote:
Originally Posted by GameOvr
Посмотреть сообщение
GTLS, I found nothing by searching like that

Why everyone not giving what i need

I need a function
"Don't explain me things just give me the function!
I will be back everytime I need you to work instead of me"
Damn we are ppl to squeeze out for free
Reply
#11

pawn Код:
new File:rflh = fopen("reports.txt", io_read);
if(rflh)
{
    new lines, id, str[256];
    while(fread(rflh, str))
    {
        lines++;
    }
    id = lines - 10;
    lines = 0;
    fseek(rflh);
    while(fread(rflh, str))
    {
        if(++lines <= id) continue;
        SendClientMessage(playerid, -1, str);
    }
    fclose(rflh);
}
Reply
#12

Quote:
Originally Posted by Y_Less
Посмотреть сообщение
There is definitely plenty of documentation online on circular buffers. We aren't here to do things for you, just help you do then yourself.
Quote:
Originally Posted by v1k1nG
Посмотреть сообщение
"Don't explain me things just give me the function!
I will be back everytime I need you to work instead of me"
Damn we are ppl to squeeze out for free
OMG why so serious why thinking in such a bad way about people you guys misunderstand me i mean give me a function by describing it like I do for others

1 example:-
Quote:
Originally Posted by GameOvr
Посмотреть сообщение
Yes, you can do something like this.

Код:
new players; //First of all you must declare a global var to save the number of players

public OnGameModeInit()
{
      if(!fexist("/Server/RegisteredPlayers.ini")) //Using File Exist function to check weather that file is there or not (In this case "if not")
      {    
             new INI:FILE = INI_Open("/Server/RegisteredPlayers.ini") // so if not we are opening file for creating
             INI_WriteInt(FILE, "Registered_Players", 0); // now we are declaring the key as Registered_Players (like Registered_Players = 0)
             INI_Close(FILE); //Closing the file
      }
      else // opposite of the first explanation
      {
             new INI:FILE = INI_Open("/Server/RegisteredPlayers.ini") //Here we are opening the file for reading
             INI_ParseFile(FILE, "LoadingRegisteredPlayers");// Native for loading files in Y_INI (Here 'FILE' represents the filepath and and the 'LoadingRegisteredPlayers' is the public function that we are using to load)
      }
}

forward LoadUserData(name[], value[]);
public LoadUserData(name[], value[])
{
      INI_Int("Registered_Players", players); // loading the number from INI file to the variable
      return 1;
}

public OnPlayerConnect(playerid)
{
       if(!registered) // IDK how you are doing this  (according to your admin system)
       {
               //So don't put "players++" Now you may think why?, The reason is what happen if somebody connects(non registered) and disconnect without registering, That's why we are not increasing the number here 
               //Goto OnPlayerDialogResponse and put 'players++' if he got registered I'm not doing that part here(I'm lazy)
       }
       return 1;
}

//Now The important thing is we must save the Number of players regged to the file OnGameModeExit
public OnGameModeExit()
{
       new INI:FILE = INI_Open("/Server/RegisteredPlayers.ini") // we are opening file for writing
       INI_WriteInt(FILE, "Registered_Players", players); 
       INI_Close(FILE); //Closing the file
}
I wanna tell you something plus


Why are you doing this?
I may ask a question
Actually does number of registered players differs according to players? Answer is NO
SO don't make the var like this, new Number_Of_Registered[MAX_PLAYERS];
Just do this, new Number_Of_Registered;
=========================================
If it was helpful...You also help me to increase my REPS...
Plz dont misunderstand others anyway thanks for the offensive reply

Quote:
Originally Posted by ReD_HunTeR
Посмотреть сообщение
pawn Код:
new File:rflh = fopen("reports.txt", io_read);
if(rflh)
{
    new lines, id, str[256];
    while(fread(rflh, str))
    {
        lines++;
    }
    id = lines - 10;
    lines = 0;
    fseek(rflh);
    while(fread(rflh, str))
    {
        if(++lines <= id) continue;
        SendClientMessage(playerid, -1, str);
    }
    fclose(rflh);
}
Thanks bro I'll try that if you can plz explain what have you done
Reply
#13

you should probably try SQL. here's an example of how simple it'd be if you wanted to do it that way.

Код:
mysql_tquery(sqlhandle, "SELECT * FROM `reports` LIMIT 10", "OnPlayerViewReports", "d", playerid);

forward OnPlayerViewReports(playerid);
public OnPlayerViewReports(playerid)
{
// yada yada
}
this is just a brief example.
Reply
#14

Quote:
Originally Posted by Infin1ty
Посмотреть сообщение
you should probably try SQL. here's an example of how simple it'd be if you wanted to do it that way.
.
Query should be,
Код:
mysql_tquery(sqlhandle, "SELECT * FROM `reports` ORDER BY `date` DESC LIMIT 10 ", "OnPlayerViewReports", "d", playerid);
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)