Reading line X from a file
#6

@leonardo1434: i think he needs some function to return a random line from a file
yours returns an already known line

@MP2: i think i might have a better optimized code here:
pawn Code:
stock ReadFileRandomLine(filename[])
{
    new File:file = fopen(filename, io_read);

    while(fread(file, str))
    {
        if(random(3) == 1)
        {
            fclose(file);
            src[strlen(src) - 1] = 0;
            return src;
        }
    }
    fclose(file);
    src[strlen(src) - 1] = 0;
    return src;
}
Now, this will loop through all the file lines (by reading them), then gets a random value between 0 - 2
if the random value is 1 then it'll return the last readed line, so in short u'll have a chance on three every iteration (u can increase it in random call)
Reply


Messages In This Thread
Reading line X from a file - by MP2 - 03.04.2013, 16:53
Re: Reading line X from a file - by OrMisicL - 03.04.2013, 17:16
Re: Reading line X from a file - by MP2 - 03.04.2013, 17:30
Re: Reading line X from a file - by leonardo1434 - 03.04.2013, 18:11
Re: Reading line X from a file - by AndreT - 03.04.2013, 18:19
Re: Reading line X from a file - by OrMisicL - 03.04.2013, 18:25
Re: Reading line X from a file - by leonardo1434 - 03.04.2013, 18:32

Forum Jump:


Users browsing this thread: 1 Guest(s)