SA-MP Forums Archive
Reading file backwards - 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: Reading file backwards (/showthread.php?tid=665266)



Reading file backwards - Suttix - 28.03.2019

Is there a way to read a log file backwards because i've been looking for solution but nothing found.
Can someone help me with that.Thanks


Re: Reading file backwards - Suttix - 28.03.2019

Anyone?


Re: Reading file backwards - TokicMajstor - 28.03.2019

If you know exact amount of characters in that (string) you can extract one by one character starting from last one, using strcat


Re: Reading file backwards - Suttix - 29.03.2019

Jok buraz,need better solution.


Re: Reading file backwards - introzen - 29.03.2019

Found this snippet by using the search function. Don't know if it's what you're looking for though.

pawn Code:
Function:Reverse(string[])
{
         // Setting vars
    new string1[256];
               lengthOfString = strlen(string);

        // Processing
    for(new i = 0; i < strlen(string); i++)
    {
        string1[i] = string[(lengthOfString-i)];
    }

        // Returning processed string
    return string1;
}



Re: Reading file backwards - Logic_ - 29.03.2019

You can try experimenting with something like:

PHP Code:
// Open "file.txt" in "read only" mode
new File:handle fopen("file.txt"io_read);
 
// If "file.txt" is open
if(handle) {
    
// Jump to the last byte of "file.txt", and print its position
    
printf("End of file position: %d"fseek(handle0seek_end));
    
fclose(handle);

( Taken from SA-MP wiki https://sampwiki.blast.hk/wiki/Fseek )


Re: Reading file backwards - Suttix - 29.03.2019

Quote:
Originally Posted by Logic_
View Post
You can try experimenting with something like:

PHP Code:
// Open "file.txt" in "read only" mode
new File:handle fopen("file.txt"io_read);
 
// If "file.txt" is open
if(handle) {
    
// Jump to the last byte of "file.txt", and print its position
    
printf("End of file position: %d"fseek(handle0seek_end));
    
fclose(handle);

( Taken from SA-MP wiki https://sampwiki.blast.hk/wiki/Fseek )
I don't know if I could use this for my log file because i need something like:
Code:
while(fread(handle, data))
but read it upwards(last 30 lines).

This is my current load function that loads file string normally from begining
PHP Code:
function givendmg_load(playerid){
    new 
f[64]; format(f64givendmg_fileGetName(playerid));
    new 
Data[256], x=0arrCoords[6][64];
    new 
File:handle fopen(fio_read);
    
    if(!
fexist(f)) return printf("File %s ne postoji te se sistem nije ucitao"f);
    
    while(
fread(handleData)){
        
split(DataarrCoords',');
        
strmid(givendmgInfo[playerid][x][dmgDate], arrCoords[0], 0strlen(arrCoords[0]));
        
strmid(givendmgInfo[playerid][x][dmgTime], arrCoords[1], 0strlen(arrCoords[1]));
        
strmid(givendmgInfo[playerid][x][dmgGivenTo], arrCoords[2], 0strlen(arrCoords[2]));
        
strmid(givendmgInfo[playerid][x][dmgGivenBy], arrCoords[3], 0strlen(arrCoords[3]));
        
givendmgInfo[playerid][x][dmgAmount] = strval(arrCoords[4]);
        
givendmgInfo[playerid][x][dmgWeapon] = strval(arrCoords[5]);
        
x++;
    }
    
fclose(handle);
    return 
1;

Can anyone give me an example how to reverse fread()


Re: Reading file backwards - Suttix - 29.03.2019

Quote:
Originally Posted by Y_Less
View Post
There is no good method. You could try fseek to get to the end. Also, English only here please (in your post at least, not code snippets).
Ok,sure.


Re: Reading file backwards - NaS - 29.03.2019

https://forum.sa-mp.com/showpost.php...82&postcount=8

This should be what you are looking for. It doesn't actually read the file backwards, but it creates a copy with reversed line order which you can open afterwards and use regular file functions.

You must define MAX_LINE_LENGTH to what you guess will be the max. line length. Unfortunately it buffers each line so that is required to know.


Re: Reading file backwards - Suttix - 29.03.2019

Quote:
Originally Posted by NaS
View Post
https://forum.sa-mp.com/showpost.php...82&postcount=8

This should be what you are looking for. It doesn't actually read the file backwards, but it creates a copy with reversed line order which you can open afterwards and use regular file functions.

You must define MAX_LINE_LENGTH to what you guess will be the max. line length. Unfortunately it buffers each line so that is required to know.
I came across on this function but i didn't know how could I use it.
I'll try it next time,thanks.