Raised This Month: $32 Target: $400
 8% 

Solved Remove line from file


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Supremache
Veteran Member
Join Date: Sep 2019
Location: Egypt
Old 02-10-2022 , 13:11   Remove line from file
Reply With Quote #1

Hello alliedmoders ,
As what i typed in the title, iam try to remove line from the file.
I created a vip system and i want to add option for remove who has date expired and i have searched for how to do that and found some ways and i used one that recommend by HamletEagle but i got an issue and don't know how to fix it

PHP Code:
RemoveExpired( const szIdentity[ ] )
{
    new const 
szTempFileName[ ] = "tempfile.ini";

    new 
szFormat128 ], szData192 ], szIdentity2MAX_NAME_LENGTH ];
    
    new 
szTempFilePath64 ];
    
formatexszTempFilePathcharsmaxszTempFilePath ), "%s/%s"g_szConfigsszTempFileName );
    
formatexszFormatcharsmaxszFormat ), "%s/%s"g_szConfigsg_iAccountFile );
    
    new 
iFilePointer fopenszFormat"rt" );
    
    if( 
iFilePointer )
    {
        new 
iInputFilePointer fopenszTempFilePath"wt" )
        if( 
iInputFilePointer )
        {
            while( 
fgetsiFilePointerszDatacharsmaxszData ) ) )
            {
                if( 
szData] != EOS )
                {
                    
parseszDataszIdentity2charsmaxszIdentity2 ) );
                    
trimszIdentity2 );
                    
                    if( !
equaliszIdentityszIdentity2 ) ) 
                    {
                        
fprintfiInputFilePointerszData );
                    }
                }
            }
            
fcloseiInputFilePointer );
            
fcloseiFilePointer );
            
delete_fileszFormat ); 
            
rename_fileszTempFilePathszFormat);
        }
    }
    return 
0;

After creating the new file with remove the line of the user who expired the file does not deleted or renamed

Another question: Is it possible to do that by using "TrieDeleteKey" ?
__________________
Youtube.com/Supremache

Bank System [Nvault - SQL Support]
VIP System
  • If you think it's that simple, then do it yourself.

Last edited by Supremache; 02-12-2022 at 17:47.
Supremache is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 02-10-2022 , 23:46   Re: Remove line from file
Reply With Quote #2

If you're trying to remove entries based on the timestamp, maybe consider using nvault since it has a built in mechanism to prune the data based on timestamps.
__________________
fysiks is offline
Supremache
Veteran Member
Join Date: Sep 2019
Location: Egypt
Old 02-11-2022 , 12:18   Re: Remove line from file
Reply With Quote #3

Quote:
Originally Posted by fysiks View Post
If you're trying to remove entries based on the timestamp, maybe consider using nvault since it has a built in mechanism to prune the data based on timestamps.
No, i created a vip system works like "Name|SteamID" "Pw" "Flags" "Expire Date", if the user has expired will lost his access but i try to add new option for remove the user from the file when expired.

I tried to use stock that i mentioned in the first post:
Code:
ReloadFile( ) {     TrieClear( g_tDatabase );         new g_szFile[ 128 ]         formatex( g_szFile, charsmax( g_szFile ), "%s/%s", g_szConfigs, g_iAccountFile )         new iFile = fopen( g_szFile, "rt" );         if( iFile )     {         new szData[ 512 ]                 while( fgets( iFile, szData, charsmax( szData ) ) )         {                trim( szData );                         switch( szData[ 0 ] )             {                 case EOS, ';''#', '/':                 {                     continue;                 }                 default:                 {                     if                     (                         parse                         (                             szData, eData[ Player_Name ]        , charsmax( eData[ Player_Name ] ),                             eData[ Player_Password ]        , charsmax( eData[ Player_Password ] ),                             eData[ Player_AccessFlags ]  , charsmax( eData[ Player_AccessFlags ] ),                             eData[ Player_Expire_Date ]  , charsmax( eData[ Player_Expire_Date ] )                         ) < 4                     )                     {                         continue;                     }                                         if( eData[ Player_Expire_Date ][ 0 ] )                     {                         if( HasDateExpired( eData[ Player_Expire_Date ] ) )                         {                             eData[ Player_Suspended ] = true;
                            RemoveExpired( eData[ Player_Name ] );
                        }                     }                                         if( eData[ Player_Name ][ 0 ] )                     {                         TrieSetArray( g_tDatabase, eData[ Player_Name ], eData, sizeof eData );                     }                                         arrayset( eData, 0, sizeof( eData ) );                 }             }         }         fclose( iFile );     } }

It's work like create a new file without line of the user who expired and delete old file and rename the new file but as you see the old file didn't deleted etc..


I'm not sure if i can do that using "TrieDeleteKey" but tried and it did not work:
PHP Code:
CheckPlayerVIPid )
{
    new 
szPassword18 ];

    
get_user_infoid"_pw"szPasswordcharsmaxszPassword ) );
        
    if( 
TrieGetArrayg_tDatabaseg_iPlayerid ][ AuthID ], eDatasizeof eData ) || TrieGetArrayg_tDatabaseg_iPlayerid ][ Name ], eDatasizeof eData ) )
    {
        if( ( 
eDataPlayer_Password ][ ] && equaleDataPlayer_Password ], szPassword ) ) || !eDataPlayer_Password ][ ] )
        {
            if( 
eDataPlayer_Suspended ]  && ! eDataPlayer_AccessFlags ][ ] )
            {
                
g_iPlayerid ][ VIP ] = 0;
                
g_iPlayerid ][ VIP ] = read_flags"z" );
                
TrieDeleteKeyg_tDatabaseeDataPlayer_Name ] )
                return 
PLUGIN_HANDLED;
            }
            else
            {
                
g_iPlayerid ][ VIP ] |= read_flagseDataPlayer_AccessFlags ] );
                return 
PLUGIN_HANDLED;
            }
        }
        else if( 
eDataPlayer_Password ][ ] && ! equaleDataPlayer_Password ], szPassword ) )
        {
            
server_cmd"kick #%d ^"You have no entry to this server^""get_user_useridid ) );   
        }
    }
    
    return 
PLUGIN_CONTINUE;

__________________
Youtube.com/Supremache

Bank System [Nvault - SQL Support]
VIP System
  • If you think it's that simple, then do it yourself.

Last edited by Supremache; 02-11-2022 at 12:21.
Supremache is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 02-11-2022 , 14:43   Re: Remove line from file
Reply With Quote #4

TrieDeleteKey() will remove the item from the trie, not the actual file. You must recreate the entire file in order to remove a line.

Not sure what's exactly wrong in your code, but I can offer you a "cleaner" way of doing it.

Instead of making a temporary file, renaming and deleting it, store all lines of your file in a dynamic array when reading it, EXCEPT the lines that are already expired.

Then, after closing the file, open it again in write mode, loop through the array and write all lines. Pseudo-code:

Code:
new szFile[256] get_configsdir(szFile, charsmax(szFile)) add(szFile, charsmax(szFile), "/YourFile.ini") // cleaner way with only 1 variable new iFilePointer = fopen(szFile, "r") // open the file in "read" mode if(iFilePointer) {     const MAX_LINE_LENGTH = 256     new Array:aArray = ArrayCreate(MAX_LINE_LENGTH)     new iLinesInFile // this will count the number of non-expired lines in the file when reading     new bool:bShouldRewrite // this will determine whether the file should be rewritten     new szData[MAX_LINE_LENGTH]     while(!feof(iFilePointer))     {         fgets(iFilePointer, szData, charsmax(szData))         trim(szData)         if(/* line is expired */)         {             // at least one line has expired - entire file needs to be rewritten in order to remove the line(s)             bShouldRewrite = true         }         else         {             // store the non-expired line in the array             iLinesInFile++             ArrayPushString(aArray, szData)         }     }     fclose(iFilePointer)     if(bShouldRewrite)     {         iFilePointer = fopen(szFile, "w") // open the file in "write" mode         for(new i; i < iLinesInFile; i++) // loop through the array and write each line in the file         {             fprintf(iFilePointer, "%a^n", ArrayGetStringHandle(aArray, i)) // %a is used to get a string directly from a dynamic array         }         fclose(iFilePointer)     }     ArrayDestroy(aArray) }
__________________

Last edited by OciXCrom; 02-12-2022 at 07:27.
OciXCrom is offline
Send a message via Skype™ to OciXCrom
Supremache
Veteran Member
Join Date: Sep 2019
Location: Egypt
Old 02-11-2022 , 20:42   Re: Remove line from file
Reply With Quote #5

I tested your code but i got a warrning message while complie the plugin in this line:
PHP Code:
fprintf(iFilePointer"%a^n"ArrayGetStringHandle(g_aFileContentsi)) 
I changed g_aFileContents to aArray but i get this error and the file rewrite as empty without store the data.
Quote:
L 02/12/2022 - 03:26:16: Invalid index 3 (count: 3)
L 02/12/2022 - 03:26:16: [AMXX] Displaying debug trace (plugin "VipSystem.amxx", version "1.0.3")
L 02/12/2022 - 03:26:16: [AMXX] Run time error 10: native error (native "ArrayGetStringHandle")
L 02/12/2022 - 03:26:16: [AMXX] [0] VIPSystem.sma::ReloadFile (line 494)
L 02/12/2022 - 03:26:16: [AMXX] [1] VIPSystem.sma::plugin_init (line 96)
__________________
Youtube.com/Supremache

Bank System [Nvault - SQL Support]
VIP System
  • If you think it's that simple, then do it yourself.

Last edited by Supremache; 02-11-2022 at 20:44.
Supremache is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 02-12-2022 , 07:27   Re: Remove line from file
Reply With Quote #6

Try removing the +1 in the loop.
Code updated.
__________________

Last edited by OciXCrom; 02-12-2022 at 07:27.
OciXCrom is offline
Send a message via Skype™ to OciXCrom
Old 02-12-2022, 08:50
Natsheh
This message has been deleted by Natsheh. Reason: Nvm
Supremache
Veteran Member
Join Date: Sep 2019
Location: Egypt
Old 02-12-2022 , 09:33   Re: Remove line from file
Reply With Quote #7

Quote:
Originally Posted by OciXCrom View Post
Try removing the +1 in the loop.
Code updated.
It's work like delete all lines except for who are not expired but i think it is not helpful because if someone put this symbol ; for disabling something will be deleted too.

Exmaple:
Quote:
#Confing file

"Pro1" "123+" "abcdefghijklmnopqrstuvwxy" "12/31/2018 00:00:00"
"Pro2" "123+" "abcdefghijklmnopqrstuvwxy" "12/31/2019 00:00:00"
;"Pro3" "123+" "abcdefghijklmnopqrstuvwxy" "12/31/2020 00:00:00"
"Pro4" "123+" "abcdefghijklmnopqrstuvwxy" "12/31/2021 00:00:00"
"Pro5" "123+" "abcdefghijklmnopqrstuvwxy" "12/31/2022 00:00:00"
"Pro6" "123+" "abcdefghijklmnopqrstuvwxy" "12/31/2023 00:00:00"
;"Pro7" "123+" "abcdefghijklmnopqrstuvwxy" "12/31/2026 00:00:00"

Quote:
"Pro5" "123+" "abcdefghijklmnopqrstuvwxy" "12/31/2022 00:00:00"
"Pro6" "123+" "abcdefghijklmnopqrstuvwxy" "12/31/2023 00:00:00"
Is there another way to do that ? or what is the wrong i did in stock that i mentioned in the first post
__________________
Youtube.com/Supremache

Bank System [Nvault - SQL Support]
VIP System
  • If you think it's that simple, then do it yourself.

Last edited by Supremache; 02-12-2022 at 09:34.
Supremache is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 02-12-2022 , 09:56   Re: Remove line from file
Reply With Quote #8

Simply check for that symbol and make an exception?... You already have a code that checks if it is a comment - store the comments in the array too.
__________________

Last edited by OciXCrom; 02-12-2022 at 09:58.
OciXCrom is offline
Send a message via Skype™ to OciXCrom
Supremache
Veteran Member
Join Date: Sep 2019
Location: Egypt
Old 02-12-2022 , 17:32   Re: Remove line from file
Reply With Quote #9

Quote:
Originally Posted by OciXCrom View Post
Simply check for that symbol and make an exception?... You already have a code that checks if it is a comment - store the comments in the array too.
I did but i checked five symbols also using array with trie in the same function i think this is not good, anyway i known a new thing.
Thank you
__________________
Youtube.com/Supremache

Bank System [Nvault - SQL Support]
VIP System
  • If you think it's that simple, then do it yourself.

Last edited by Supremache; 02-12-2022 at 17:34.
Supremache 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 19:29.


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