AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   read file small code (https://forums.alliedmods.net/showthread.php?t=320729)

Sanjay Singh 01-06-2020 05:42

read file small code
 
can you guys tell me whats diff in both codes?
which one will be better to use.

Code1:
PHP Code:

public filereadtest(id)
{
    new 
file fopen(filename,"r")
    if(
file)
    {
        new 
data[128]
        while(
fgets(filedata127))
        {
            
parse(datadata127)
            
server_cmd("say ^"%s^""data);
        }
        
fclose(file)
    }


Code2:
PHP Code:

public filereadtest(id)
{
    new 
file fopen(filename,"r")
    if(
file)
    {
        new 
data[128]
        while(!
feof(file))
        {
            
fgets(filedata127)

            
parse(datadata127)
            
server_cmd("say ^"%s^""data);
        }
        
fclose(file)
    }



redivcram 01-06-2020 05:52

Re: read file small code
 
Code:

feof
returns whether the end of the file has been reached.
Code:

fgets
does the same by indexing each line every time its called (from which feof can determine) in the file and outputs its content by reference.

Sanjay Singh 01-06-2020 06:00

Re: read file small code
 
Quote:

Originally Posted by redivcram (Post 2679116)
Code:

feof
returns whether the end of the file has been reached.
Code:

fgets
does the same by indexing each line every time its called (from which feof can determine) in the file and outputs its content by reference.

so which will be better? fgets?

redivcram 01-06-2020 06:42

Re: read file small code
 
In your case, feof is redundant, by how I've tried to explain both. So fgets.

HamletEagle 01-06-2020 10:44

Re: read file small code
 
If you are asking which one is more efficient: IT DOES NOT MATTER.

Bugsy 01-06-2020 17:38

Re: read file small code
 
I think the fgets() method gets the nod to efficiency. Would it ever be noticeable? no, but on paper it is more efficient.

fysiks 01-06-2020 20:58

Re: read file small code
 
I remember someone posting that feof() shouldn't be used for the condition in a while loop like this for some low-level reason. I'd have to search around to see if I could find it again.

Found it: https://forums.alliedmods.net/showthread.php?t=310086

Sanjay Singh 01-07-2020 05:48

Re: read file small code
 
Thanks :)


All times are GMT -4. The time now is 02:41.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.