Reading file backwards
#1

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

Anyone?
Reply
#3

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

Jok buraz,need better solution.
Reply
#5

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

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

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()
Reply
#8

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

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

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


Forum Jump:


Users browsing this thread: 1 Guest(s)