AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   how do i delete a line from a file? [solved] (https://forums.alliedmods.net/showthread.php?t=87369)

Owyn 03-10-2009 15:46

how do i delete a line from a file? [solved]
 
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? ^_^

Exolent[jNr] 03-10-2009 15:54

Re: how do i delete a line from a file?
 
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

AntiBots 03-10-2009 16:02

Re: how do i delete a line from a file?
 
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

Owyn 03-10-2009 16:10

Re: how do i delete a line from a file?
 
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?

Exolent[jNr] 03-10-2009 16:26

Re: how do i delete a line from a file?
 
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.

Owyn 03-10-2009 16:29

Re: how do i delete a line from a file?
 
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) ) { }
}


Exolent[jNr] 03-10-2009 16:45

Re: how do i delete a line from a file?
 
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) ) { } }

Owyn 03-10-2009 16:59

Re: how do i delete a line from a file?
 
this code kills server too =\

Exolent[jNr] 03-10-2009 18:21

Re: how do i delete a line from a file? [still need help]
 
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.

Owyn 03-10-2009 18:26

Re: how do i delete a line from a file? [still need help]
 
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)


All times are GMT -4. The time now is 09:02.

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