PDA

View Full Version : Reading file backwards


Suttix
28/03/2019, 06:18 PM
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

Suttix
28/03/2019, 09:02 PM
Anyone?

TokicMajstor
28/03/2019, 11:00 PM
If you know exact amount of characters in that (string) you can extract one by one character starting from last one, using strcat

Suttix
29/03/2019, 12:23 AM
Jok buraz,need better solution.

introzen
29/03/2019, 08:44 AM
Found this snippet by using the search function. Don't know if it's what you're looking for though.

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;
}

Logic_
29/03/2019, 09:24 AM
You can try experimenting with something like:

// 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(handle, 0, seek_end));

fclose(handle);
}

( Taken from SA-MP wiki https://wiki.sa-mp.com/wiki/Fseek )

Suttix
29/03/2019, 11:24 AM
You can try experimenting with something like:

// 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(handle, 0, seek_end));

fclose(handle);
}

( Taken from SA-MP wiki https://wiki.sa-mp.com/wiki/Fseek )

I don't know if I could use this for my log file because i need something like: while(fread(handle, data)) but read it upwards(last 30 lines).

This is my current load function that loads file string normally from begining
function givendmg_load(playerid){
new f[64]; format(f, 64, givendmg_file, GetName(playerid));
new Data[256], x=0, arrCoords[6][64];

new File:handle = fopen(f, io_read);

if(!fexist(f)) return printf("File %s ne postoji te se sistem nije ucitao", f);

while(fread(handle, Data)){
split(Data, arrCoords, ',');
strmid(givendmgInfo[playerid][x][dmgDate], arrCoords[0], 0, strlen(arrCoords[0]));
strmid(givendmgInfo[playerid][x][dmgTime], arrCoords[1], 0, strlen(arrCoords[1]));
strmid(givendmgInfo[playerid][x][dmgGivenTo], arrCoords[2], 0, strlen(arrCoords[2]));
strmid(givendmgInfo[playerid][x][dmgGivenBy], arrCoords[3], 0, strlen(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() :D

Suttix
29/03/2019, 11:59 AM
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.

NaS
29/03/2019, 01:59 PM
https://forum.sa-mp.com/showpost.php?p=3887482&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.

Suttix
29/03/2019, 03:17 PM
https://forum.sa-mp.com/showpost.php?p=3887482&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.