fread - read lines with time gap
#1

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

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

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

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

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

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
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)