SA-MP Forums Archive
fread - read lines with time gap - 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: fread - read lines with time gap (/showthread.php?tid=617175)



fread - read lines with time gap - ranme15 - 16.09.2016

ay,

I am interested in reading some lines from a file with a delay between each line using while(fread).
Any ideas to make that happen? (using a timer obviously)

greetings


Re: fread - read lines with time gap - Nero_3D - 16.09.2016

Like you said, use a timer to call fread and close it afterwards, also why?


Re: fread - read lines with time gap - ranme15 - 16.09.2016

Quote:
Originally Posted by Nero_3D
Посмотреть сообщение
Like you said, use a timer to call fread and close it afterwards, also why?
That doesn't help considering I will have to start reading from a specific line which is basically impossible.
I have alot of lines to read and it takes some time to read & put some actions on each line within one step.


Re: fread - read lines with time gap - Spmn - 16.09.2016

Quote:
Originally Posted by ranme15
Посмотреть сообщение
That doesn't help considering I will have to start reading from a specific line which is basically impossible.
I have alot of lines to read and it takes some time to read & put some actions on each line within one step.
https://sampwiki.blast.hk/wiki/Fseek

A more simplier way is to open the file outside the timer function and pass the file handle to read timer. Then you close the file when it is completely read (i.e fread returns 0)
Doing so, you won't have to open&seek&close the file at every timer call.


Pseudo code example:
Код:
ProcessFile(filename[], interval)
{
    new File: handle = fopen(filename, io_read);
    if(handle) SetTimerEx("ReadLines", interval, 0, "dd", handle, interval);
}

ReadLines(File: handle, interval)
{
    new string[128];
    if(fread(handle, string))
    {
        // do whatever

        SetTimerEx("ReadLines", interval, 0, "dd", handle, interval); // continue reading
    }
    else // nothing left unread
    {
        fclose(handle);
    }
}



Re: fread - read lines with time gap - PrawkC - 16.09.2016

Quote:
Originally Posted by Spmn
Посмотреть сообщение
https://sampwiki.blast.hk/wiki/Fseek

A more simplier way is to open the file outside the timer function and pass the file handle to read timer. Then you close the file when it is completely read (i.e fread returns 0)
Doing so, you won't have to open&seek&close the file at every timer call.


Pseudo code example:
Код:
ProcessFile(filename[], interval)
{
    new File: handle = fopen(filename, io_read);
    if(handle) SetTimerEx("ReadLines", interval, 0, "dd", handle, interval);
}

ReadLines(File: handle, interval)
{
    new string[128];
    if(fread(handle, string))
    {
        // do whatever

        SetTimerEx("ReadLines", interval, 0, "dd", handle, interval); // continue reading
    }
    else // nothing left unread
    {
        fclose(handle);
    }
}
Pretty sure this will hang the server.


Re: fread - read lines with time gap - ranme15 - 16.09.2016

Quote:
Originally Posted by Spmn
Посмотреть сообщение
https://sampwiki.blast.hk/wiki/Fseek

A more simplier way is to open the file outside the timer function and pass the file handle to read timer. Then you close the file when it is completely read (i.e fread returns 0)
Doing so, you won't have to open&seek&close the file at every timer call.


Pseudo code example:
Код:
ProcessFile(filename[], interval)
{
    new File: handle = fopen(filename, io_read);
    if(handle) SetTimerEx("ReadLines", interval, 0, "dd", handle, interval);
}

ReadLines(File: handle, interval)
{
    new string[128];
    if(fread(handle, string))
    {
        // do whatever

        SetTimerEx("ReadLines", interval, 0, "dd", handle, interval); // continue reading
    }
    else // nothing left unread
    {
        fclose(handle);
    }
}
I will check it out tomorrow and update ya.
thanks & night