Raised This Month: $ Target: $400
 0% 

Reading from a file


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
killergirl
Senior Member
Join Date: Jul 2010
Old 03-28-2011 , 11:37   Reading from a file
Reply With Quote #1

I'm trying to read from a text file "ppl.txt", it's working only for the first line (Player1). If I try with Player2 or with KillerGirl it fails.

I think the the script is reading only the first line. How can I make to read the rest of lines?

ppl.txt:
Code:
Player1
Player2
KillerGirl
PHP Code:
#include <amxmodx>
#include <amxmisc>

new g_File[255]

public 
plugin_init()
{
    
register_plugin("TEST","1.0","KG")
    
    
register_concmd("say dsa","asd_test")
    
    
get_configsdir(g_File254)
    
format(g_File254"%s/ppl.txt"g_File)    
}

public 
asd_test(id)
{    
    new 
FileTextOpenerNewUserName[32], OldUserName[32], ReadData[128]
    
    
FileTextOpener fopen(g_File"r")
    
    if(
FileTextOpener)
    {
        while(
fgets(FileTextOpener,ReadData,127))
        {
            
parse(ReadDataOldUserName31)
            
            
get_user_name(idNewUserName31)
            
            if(
equal(NewUserNameOldUserName))
            {
                
client_print(idprint_chat"YUPIIII ! %s"NewUserName)
                break;
            }
            else
            {
                
client_print(idprint_chat"FAIL ! %s"NewUserName)
                break;
            }
        }
        
fclose(FileTextOpener)
    }
    
    return 
PLUGIN_HANDLED

killergirl is offline
wrecked_
Veteran Member
Join Date: Jan 2010
Location: New York (GMT-5)
Old 03-28-2011 , 11:41   Re: Reading from a file
Reply With Quote #2

Instead of using fgets() within the while condition, you should use it within the brackets and use feof() for the condition.

ex:
Code:
while( fgets() ) {     // ... }
->
Code:
while( !feof() ) // the ! is making sure it's not the end of the file {     fgets()     // ... }
__________________
[ Paid Requests ]
DO NOT PM ME ABOUT BLOCKMAKER
NO PRIVATE SUPPORT
wrecked_ is offline
killergirl
Senior Member
Join Date: Jul 2010
Old 03-28-2011 , 11:59   Re: Reading from a file
Reply With Quote #3

PHP Code:
public asd_test(id)
{    
    new 
FileTextOpenerNewUserName[32], OldUserName[32], ReadData[128]
    
    
FileTextOpener fopen(g_File"r")
    
    if(
FileTextOpener)
    {
        while( !
feof(FileTextOpener) )
        {
            
fgets(FileTextOpener,ReadData,127)
            {
                
parse(ReadDataOldUserName31)
                
                
get_user_name(idNewUserName31)
                
                if(
equal(NewUserNameOldUserName))
                {
                    
client_print(idprint_chat"YES ! %s"NewUserName)
                    break;
                }
                else
                {
                    
client_print(idprint_chat"NO ! %s"NewUserName)
                    break;
                }
            }
        }
        
fclose(FileTextOpener)
    }
    
    return 
PLUGIN_HANDLED

Same.
killergirl is offline
wrecked_
Veteran Member
Join Date: Jan 2010
Location: New York (GMT-5)
Old 03-28-2011 , 12:14   Re: Reading from a file
Reply With Quote #4

  • The whole reason the code is only executing one iteration of the while loop is because you are breaking from the loop after it executes once. Remove both of your "break;" statements and it will be fine.
  • The bracket directly under fgets() and the corresponding bracket are not needed as it isn't a conditional statement.
  • I recommend you use the flags "rt" for the second argument of fopen().
  • Why are you using parse()?
__________________
[ Paid Requests ]
DO NOT PM ME ABOUT BLOCKMAKER
NO PRIVATE SUPPORT
wrecked_ is offline
schmurgel1983
Veteran Member
Join Date: Aug 2006
Location: Germany
Old 03-28-2011 , 12:31   Re: Reading from a file
Reply With Quote #5

maybe this can help u a little bit
__________________

Working on:
nothing
schmurgel1983 is offline
killergirl
Senior Member
Join Date: Jul 2010
Old 03-28-2011 , 12:34   Re: Reading from a file
Reply With Quote #6

This is why I break the loop:

[IMG]http://img856.**************/img856/7668/86684968.png[/IMG]

I used parse because the tutorial:
http://forums.alliedmods.net/showpos...96&postcount=1

Flags:

Mode uses the standard C library of mode types.
The first character can be:
"a" - append
"r" - read
"w" - write

The second character can be:
"t" - text
"b" - binary


Thanks !

PHP Code:
public asd_test(id)
{    
    new 
FileTextOpenerNewUserName[32], OldUserName[32], ReadData[128]
    
    
FileTextOpener fopen(g_File"rt")
    
    if(
FileTextOpener)
    {
        while( !
feof(FileTextOpener) )
        {
            
fgets(FileTextOpener,ReadData,127)
                
            
get_user_name(idNewUserName31)
                
            if(
equal(NewUserNameOldUserName))
            {
                
client_print(idprint_chat"YES ! %s"NewUserName)
            }
            else
            {
                
client_print(idprint_chat"NO ! %s"NewUserName)
            }
        }
        
fclose(FileTextOpener)
    }
    
    return 
PLUGIN_HANDLED

Same, the script won't work
killergirl is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 03-28-2011 , 13:50   Re: Reading from a file
Reply With Quote #7

Quote:
Originally Posted by wrecked_ View Post
Instead of using fgets() within the while condition, you should use it within the brackets and use feof() for the condition.

ex:
Code:
while( fgets() ) {     // ... }
->
Code:
while( !feof() ) // the ! is making sure it's not the end of the file {     fgets()     // ... }
That won't make a difference.
fgets() is perfectly fine to use as the condition in the loop.

@killergirl
Use trim() after fgets() so the new line character is removed from the end of the string.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 03-28-2011 , 19:01   Re: Reading from a file
Reply With Quote #8

Quote:
Originally Posted by killergirl View Post
This is why I break the loop:

[IMG]http://img856.**************/img856/7668/86684968.png[/IMG]

PHP Code:
public asd_test(id)
{    
    new 
FileTextOpenerNewUserName[32], OldUserName[32], ReadData[128]
    
    
FileTextOpener fopen(g_File"rt")
    
    if(
FileTextOpener)
    {
        while( !
feof(FileTextOpener) )
        {
            
fgets(FileTextOpener,ReadData,127)
                
            
get_user_name(idNewUserName31)
                
            if(
equal(NewUserNameOldUserName))
            {
                
client_print(idprint_chat"YES ! %s"NewUserName)
            }
            else
            {
                
client_print(idprint_chat"NO ! %s"NewUserName)
            }
        }
        
fclose(FileTextOpener)
    }
    
    return 
PLUGIN_HANDLED

Same, the script won't work
To prevent all that chat messages you need to move the client_print() outside of the loop and do something like:

PHP Code:
if( equal(newnameoldname) )
{
    
boolvariable true
    
break

Then, check the boolvariable and print yes or no after the loop is over.
__________________
fysiks is offline
madeitout
Member
Join Date: Jun 2008
Old 03-29-2011 , 15:32   Re: Reading from a file
Reply With Quote #9

OldUserName is not getting set
PHP Code:
public asd_test(id)
{   
    new 
FileTextOpenerNewUserName[32], OldUserName[32]
   
    
FileTextOpener fopen(g_File"rt")
   
    if (
FileTextOpener)
    {
        
get_user_name(idNewUserName31)
        while( !
feof(FileTextOpener) )
        {
            
fgets(FileTextOpenerOldUserName31)
            
trim(OldUserName)

            if (
equal(NewUserNameOldUserName))
            {
                
client_print(idprint_chat"YES ! %s"NewUserName)
                break;
            }
            else
            {
                
client_print(idprint_chat"NO ! %s"NewUserName)
            }
        }
        
fclose(FileTextOpener)
    }
   
    return 
PLUGIN_HANDLED

__________________
madeitout is offline
killergirl
Senior Member
Join Date: Jul 2010
Old 03-29-2011 , 15:48   Re: Reading from a file
Reply With Quote #10

Now it's working !

I removed brackets from fgets(), I added trim and I moved the "print to player" function outside controlled by a bool variable. I added a task function to call the "print to player".

Thank you for support !
killergirl is offline
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 14:29.


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