Raised This Month: $ Target: $400
 0% 

how do i delete a line from a file? [solved]


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Owyn
Veteran Member
Join Date: Nov 2007
Old 03-10-2009 , 15:46   how do i delete a line from a file? [solved]
Reply With Quote #1

Code:
new f = fopen(g_file, "a+");
    
    fprintf(f, "^"%s^" ^"%s^"^n",\
        arg1,\
        arg2
        );
    
    fclose(f);
i use this code to write lines, but i need to delete such a line, any ideas? ^_^
Owyn is offline
Send a message via ICQ to Owyn
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 03-10-2009 , 15:54   Re: how do i delete a line from a file?
Reply With Quote #2

I did this in my FVault include.

Code:
/**  * Removes a key from a vault  *  * @param vaultname Vault name to look in  * @param key       Key to remove  * @return      No return  */ stock fvault_remove_key(const vaultname[], const key[]) {     static const temp_vault_name[] = "fvault_remove_key.txt";         new file = fopen(temp_vault_name, "wt");         new filename[128];     _FormatVaultName(vaultname, filename, sizeof(filename) - 1);         new vault = fopen(filename, "rt");         new _data[512], _key[64], _other[3];         while( !feof(vault) )     {         fgets(vault, _data, sizeof(_data) - 1);         parse(_data, _key, sizeof(_key) - 1, _other, sizeof(_other) - 1);                 if( !equal(_key, key) )         {             fputs(file, _data);         }     }         fclose(file);     fclose(vault);         delete_file(filename);         while( !rename_file(temp_vault_name, filename, 1) ) { }         //delete_file(temp_vault_name); }

http://forums.alliedmods.net/showthread.php?t=76453
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
AntiBots
Veteran Member
Join Date: May 2008
Location: Brazil
Old 03-10-2009 , 16:02   Re: how do i delete a line from a file?
Reply With Quote #3

Well using exolent method I do this. PS: I dont test

PHP Code:
stock remove_x_line(const file[], line)
{
    new 
extencion[8], realfile[128], tempfile[128], lencontador
    copy
(realfile127file)
    
copy(tempfile127file)
    
len strlen(tempfile)
    
copy(extencion7tempfile[len-4])
    
tempfile[len-4] = '^0'
    
formatex(tempfile127"%s_temp%s"tempfileextencion)
    
    new 
file fopen(tempfile"wt")
    new 
vault fopen(realfile"rt")
    
    new 
data[128]
    
    while( !
feof(vault) )
    {
        
contador++
        
fgets(vaultdata127)
        
        if( 
contador == line ) continue
        
        
fputs(filedata)
    }
    
    
fclose(file)
    
fclose(vault)
    
    
delete_file(realfile)
    while( !
rename_file(tempfilerealfile1) ) { }

Well i dont remove, but exolent ansers first. Sorry xD
__________________
AntiBots is offline
Send a message via ICQ to AntiBots Send a message via MSN to AntiBots Send a message via Skype™ to AntiBots
Owyn
Veteran Member
Join Date: Nov 2007
Old 03-10-2009 , 16:10   Re: how do i delete a line from a file?
Reply With Quote #4

what is the contador?
as i understand this func need to be provided by a whole line to compare it with lines find it and delete it?
Owyn is offline
Send a message via ICQ to Owyn
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 03-10-2009 , 16:26   Re: how do i delete a line from a file?
Reply With Quote #5

All you have to do, is create a new file, and copy the contents of the current file to the new one except for the lines you don't want.
When you are done, close the files, delete the original file, and rename the new file to the original.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Owyn
Veteran Member
Join Date: Nov 2007
Old 03-10-2009 , 16:29   Re: how do i delete a line from a file?
Reply With Quote #6

i tried to modyfy that stock but on execution it killed my test server =3

Code:
stock unregname(const arg2[])
{
    new extencion[8], realfile[128], tempfile[128], len
    new arg11[32], arg12[32]
    copy(realfile, 127, g_steam_file)
    copy(tempfile, 127, g_steam_file)
    len = strlen(tempfile)
    copy(extencion, 7, tempfile[len-4])
    tempfile[len-4] = '^0'
    formatex(tempfile, 127, "%s_temp%s", tempfile, extencion)
    
    new file = fopen(tempfile, "wt")
    new vault = fopen(realfile, "rt")
    
    new data[128]
    
    while( !feof(vault) )
    {
        fgets(file, data, 63);
        
        parse(data, arg11, 31, arg12, 31);
        
        if( arg12[31] == arg2[31] ) continue
        
        fputs(file, data)
    }
    
    fclose(file)
    fclose(vault)
    
    delete_file(realfile)
    while( !rename_file(tempfile, realfile, 1) ) { }
}

Last edited by Owyn; 03-10-2009 at 16:42.
Owyn is offline
Send a message via ICQ to Owyn
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 03-10-2009 , 16:45   Re: how do i delete a line from a file?
Reply With Quote #7

Code:
stock unregname(const arg2[]) {     new extencion[8], realfile[128], tempfile[128], len     new arg11[32], arg12[32]     copy(realfile, 127, g_steam_file)     copy(tempfile, 127, g_steam_file)     len = strlen(tempfile)     copy(extencion, 7, tempfile[len-4])     tempfile[len-4] = '^0'     format(tempfile, 127, "%s_temp%s", tempfile, extencion)         new file = fopen(tempfile, "wt")     new vault = fopen(realfile, "rt")         new data[128]         while( !feof(vault) )     {         fgets(vault, data, 63);                 parse(data, arg11, 31, arg12, 31);                 if( equal(arg12[31], arg2[31]) ) continue                 fputs(file, data)     }         fclose(file)     fclose(vault)         delete_file(realfile)     while( !rename_file(tempfile, realfile, 1) ) { } }
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!

Last edited by Exolent[jNr]; 03-10-2009 at 18:20.
Exolent[jNr] is offline
Owyn
Veteran Member
Join Date: Nov 2007
Old 03-10-2009 , 16:59   Re: how do i delete a line from a file?
Reply With Quote #8

this code kills server too =\

Last edited by Owyn; 03-10-2009 at 17:24.
Owyn is offline
Send a message via ICQ to Owyn
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 03-10-2009 , 18:21   Re: how do i delete a line from a file? [still need help]
Reply With Quote #9

Try that.
If that doesn't work, then there is a problem with rename_file(), which means you don't have permission to rename the file.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Owyn
Veteran Member
Join Date: Nov 2007
Old 03-10-2009 , 18:26   Re: how do i delete a line from a file? [still need help]
Reply With Quote #10

thx, but i think im gonna use arrays, cuz it's not good opening a file everytime som1 connects
*can't give rep atm, need to spread some first)
Owyn is offline
Send a message via ICQ to Owyn
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 09:02.


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