Raised This Month: $ Target: $400
 0% 

Solved Remove line from file


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 02-11-2022 , 14:43   Re: Remove line from file
Reply With Quote #1

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 #2

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
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 11:43.


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