AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved Duplicated reading? (https://forums.alliedmods.net/showthread.php?t=310086)

edon1337 08-18-2018 08:42

Duplicated reading?
 
For some reason my code gets duplicated, so if the original line that we have is like this
PHP Code:

;"Name" "Pw" "AccessFlags" "Prefix" 

It becomes

PHP Code:

"Name" "Pw" "AccessFlags" "Prefix"
"Name" "Pw" "AccessFlags" "Prefix"
// blank line 

Instead of just (removed ;)
PHP Code:

"Name" "Pw" "AccessFlags" "Prefix" 

Basically #1 log gets called twice while it should be called once, and I have no idea what makes the file read the same line twice.

PHP Code:

UnSuspendPlayer( const id )
{
    new const 
szTempFileName[ ] = "tempfile.ini";

    new 
szConfigs32 ], szFormat64 ], szData128 ], szDataName32 ], szDataPw32 ], szDataFlags32 ], szDataPrefix32 ], szPlayerName32 ];
    
get_configsdirszConfigscharsmaxszConfigs ) );
    
    new 
szTempFilePath256 ]; 
    
formatexszTempFilePathcharsmaxszTempFilePath ), "%s/%s"szConfigsszTempFileName );
    
    
get_user_nameidszPlayerNamecharsmaxszPlayerName ) );
    
    
formatexszFormatcharsmaxszFormat ), "%s/%s"szConfigsg_szFile );
    new 
iFilePointer fopenszFormat"rt" );
    
    if( 
iFilePointer )
    {
        new 
iInputFilePointer fopenszTempFilePath"wt" );
        
        if( 
iInputFilePointer )
        {
            while( ! 
feofiFilePointer ) )
            {            
                
fgetsiFilePointerszDatacharsmaxszData ) );
                
trimszData );
                
                
parseszDataszDataNamecharsmaxszDataName ), szDataPwcharsmaxszDataPw ), szDataFlagscharsmaxszDataFlags ), szDataPrefixcharsmaxszDataPrefix ) );
                
                
log_to_file"unsusp.txt""#1 Called" );
                                
                if( 
szDataName] == ';' // check if name has ; in it (;"DoNii" becomes ";DoNii")
                
{                    
                    if( 
equalszDataName], szPlayerName ) ) // found the admin that we're searching for
                    
{
                        
fprintfiInputFilePointer"^"%s^" ^"%s^" ^"%s^" ^"%s^"^n"szDataName], szDataPwszDataFlagsszDataPrefix ); // write to new file without ;
                        
log_to_file"unsusp.txt""#3 %s"szDataName );
                    }
                    
                    else 
// not the person we're looking for
                    
{
                        
fprintfiInputFilePointer";^"%s^" ^"%s^" ^"%s^" ^"%s^"^n"szDataNameszDataPwszDataFlagsszDataPrefix ); // write as it was before
                        
log_to_file"unsusp.txt""#3 %s"szDataName );
                    }
                }
                
                else 
// doesnt contain ;
                
{
                    
fprintfiInputFilePointer"^"%s^" ^"%s^" ^"%s^" ^"%s^"^n"szDataNameszDataPwszDataFlagsszDataPrefix ); // unknown admin, write as is
                    
log_to_file"unsusp.txt""#2 %s"szDataName );
                }
            }
            
fcloseiInputFilePointer );
            
fcloseiFilePointer );

            
delete_fileszFormat );
            
rename_fileszTempFilePathszFormat);
        }
    }
    return 
0;


Code:

L 08/18/2018 - 15:29:04: #1 Called
L 08/18/2018 - 15:29:04: #3 DoNii
L 08/18/2018 - 15:29:04: #1 Called
L 08/18/2018 - 15:29:04: #2 DoNii


PartialCloning 08-18-2018 09:48

Re: Duplicated reading?
 
To simplify your code you can use "if(szDataName[0] == ';')" and "equal(szDataName[1], szPlayerName )" without having to remove the semicolon.

edon1337 08-18-2018 10:28

Re: Duplicated reading?
 
Quote:

Originally Posted by PartialCloning (Post 2610917)
To simplify your code you can use "if(szDataName[0] == ';')" and "equal(szDataName[1], szPlayerName )" without having to remove the semicolon.

Thanks for the optimization

klippy 08-18-2018 11:31

Re: Duplicated reading?
 
Instead of
PHP Code:

while( ! feofiFilePointer ) ) 

try
PHP Code:

while( fgetsiFilePointerszDatacharsmaxszData ) ) ) 

and remove the fgets call from below that line.

edon1337 08-18-2018 12:39

Re: Duplicated reading?
 
Quote:

Originally Posted by KliPPy (Post 2610934)
Instead of
PHP Code:

while( ! feofiFilePointer ) ) 

try
PHP Code:

while( fgetsiFilePointerszDatacharsmaxszData ) ) ) 

and remove the fgets call from below that line.

Worked, thanks, what causes feof to do that?

klippy 08-18-2018 14:20

Re: Duplicated reading?
 
Google "while feof" (it's same in C) , it's a common problem.

edon1337 08-18-2018 14:54

Re: Duplicated reading?
 
Quote:

Originally Posted by KliPPy (Post 2610957)
Google "while feof" (it's same in C) , it's a common problem.

Everyone uses it though

klippy 08-18-2018 15:03

Re: Duplicated reading?
 
They shouldn't for reading a file line by line.

edon1337 08-18-2018 15:05

Re: Duplicated reading?
 
Quote:

Originally Posted by KliPPy (Post 2610966)
They shouldn't for reading a file line by line.

I have a question related to this,

let's say we have this line
PHP Code:

;"Msg1" "Msg2" "Msg3" "Msg4" // Important info 

Now that code will turn that into this
PHP Code:

"Msg1" "Msg2" "Msg3" "Msg4" 

Any chance we can transfer // Important info to the new file too?

HamletEagle 08-18-2018 15:23

Re: Duplicated reading?
 
What do you mean? The entire line should be copied.


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

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