AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Reading from a file (https://forums.alliedmods.net/showthread.php?t=153735)

killergirl 03-28-2011 11:37

Reading from a file
 
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



wrecked_ 03-28-2011 11:41

Re: Reading from a file
 
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()     // ... }

killergirl 03-28-2011 11:59

Re: Reading from a file
 
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.

wrecked_ 03-28-2011 12:14

Re: Reading from a file
 
  • 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()?

schmurgel1983 03-28-2011 12:31

Re: Reading from a file
 
maybe this can help u a little bit

killergirl 03-28-2011 12:34

Re: Reading from a file
 
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 :cry:

Exolent[jNr] 03-28-2011 13:50

Re: Reading from a file
 
Quote:

Originally Posted by wrecked_ (Post 1440691)
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.

fysiks 03-28-2011 19:01

Re: Reading from a file
 
Quote:

Originally Posted by killergirl (Post 1440723)
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 :cry:

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.

madeitout 03-29-2011 15:32

Re: Reading from a file
 
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



killergirl 03-29-2011 15:48

Re: Reading from a file
 
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 !


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

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