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



File Reading - K0P - 22.07.2016

How to get last 4 lines of a file?

forexample i have this data in the file:

Код:
1
2
3
4
5
How to get the last 4 lines of that file?


Re: File Reading - Jefff - 22.07.2016

Open file then read whole + count lines, next use fseek(handle, 0, seek_start); and read again whole file and if counter >= idx - 4 show line for player ;d


Re: File Reading - K0P - 22.07.2016

Quote:
Originally Posted by Jefff
Посмотреть сообщение
Open file then read whole + count lines, next use fseek(handle, 0, seek_start); and read again whole file and if counter >= idx - 4 show line for player ;d
Can you please give a simple code example?


Re: File Reading - Jefff - 22.07.2016

pawn Код:
new str[128],lines,idx;
new File:F = fopen("file.txt",io_read);
while(fread(F, str))
    lines++;

if(lines > 3)
{
    fseek(F, 0);

    while(fread(F, str))
    {
        if(++idx > lines-4)
            print(str);
    }
}
fclose(F);



Re: File Reading - K0P - 22.07.2016

Quote:
Originally Posted by Jefff
Посмотреть сообщение
pawn Код:
new str[128],lines,idx;
new File:F = fopen("file.txt",io_read);
while(fread(F, str))
    lines++;

if(lines > 3)
{
    fseek(F, 0, seek_start);

    while(fread(F, str))
    {
        if(++idx > lines-4)
            print(str);
    }
}
fclose(F);
Thanks
And ofc +Rep