Raised This Month: $51 Target: $400
 12% 

read file small code


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Sanjay Singh
Veteran Member
Join Date: Sep 2016
Old 01-06-2020 , 05:42   read file small code
Reply With Quote #1

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)
    }

__________________

Last edited by Sanjay Singh; 01-06-2020 at 05:44.
Sanjay Singh is offline
Send a message via AIM to Sanjay Singh
redivcram
Veteran Member
Join Date: Jul 2014
Location: Serbia
Old 01-06-2020 , 05:52   Re: read file small code
Reply With Quote #2

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.

Last edited by redivcram; 01-06-2020 at 05:54.
redivcram is offline
Sanjay Singh
Veteran Member
Join Date: Sep 2016
Old 01-06-2020 , 06:00   Re: read file small code
Reply With Quote #3

Quote:
Originally Posted by redivcram View Post
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?
__________________
Sanjay Singh is offline
Send a message via AIM to Sanjay Singh
redivcram
Veteran Member
Join Date: Jul 2014
Location: Serbia
Old 01-06-2020 , 06:42   Re: read file small code
Reply With Quote #4

In your case, feof is redundant, by how I've tried to explain both. So fgets.

Last edited by redivcram; 01-06-2020 at 06:42.
redivcram is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 01-06-2020 , 10:44   Re: read file small code
Reply With Quote #5

If you are asking which one is more efficient: IT DOES NOT MATTER.
__________________
HamletEagle is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-06-2020 , 17:38   Re: read file small code
Reply With Quote #6

I think the fgets() method gets the nod to efficiency. Would it ever be noticeable? no, but on paper it is more efficient.
__________________
Bugsy is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 01-06-2020 , 20:58   Re: read file small code
Reply With Quote #7

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
__________________

Last edited by fysiks; 01-06-2020 at 21:43.
fysiks is offline
Sanjay Singh
Veteran Member
Join Date: Sep 2016
Old 01-07-2020 , 05:48   Re: read file small code
Reply With Quote #8

Thanks
__________________
Sanjay Singh is offline
Send a message via AIM to Sanjay Singh
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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