View Single Post
D4rkSiD3Rs
Senior Member
Join Date: Jan 2018
Location: Morocco
Old 10-11-2018 , 15:06   Re: Modify a file using another file
Reply With Quote #7

Quote:
Originally Posted by Bugsy View Post
You need to scan the files and write to a new file if your condition is met; once done, you then delete file1 and rename the new file to the file1 filename. With your code you are adding lines to the existing file.

I've always had issues with delete_file() failing. Can anyone weigh in on what the problem is? The tmpFile.tmp file is accurate regarding contents, but the deletion of the old file1 and renaming of temp to file1 fails.

PHP Code:
#include <amxmodx>
#include <amxmisc>

public plugin_init()
{
    
register_plugin("File Cleaner""1.0""~D4rkSiD3Rs~");
    
CleanFile();
}

public 
CleanFile()
{
    new 
iFile1 iFile2 iFile3 szConfigsDir64 ] , szFile1100 ] , szFile2100 ] , szFile3100 ];
    new 
szLine136 ] , szLine236 ] , szName132 ] , szName232 ] , szOption];
    
    
get_configsdirszConfigsDir charsmaxszConfigsDir ) );

    
formatexszFile1 charsmaxszFile1 ) , "%s/TestFile1.txt" szConfigsDir );
    
formatexszFile2 charsmaxszFile2 ) , "%s/TestFile2.txt" szConfigsDir );
    
formatexszFile3 charsmaxszFile3 ) , "%s/tmpFile.tmp" szConfigsDir );
    
    
iFile1 fopenszFile1 "rt" );
    
iFile2 fopenszFile2 "rt" );
    
iFile3 fopenszFile3 "wt" );
    
    while ( 
fgetsiFile1 szLine1 charsmaxszLine1 ) ) )
    {
        
parseszLine1 szName1 charsmaxszName1 ) );
        
        while ( 
fgetsiFile2 szLine2 charsmaxszLine2 ) ) )
        {
            
parseszLine2 szName2 charsmaxszName2 ) , szOption charsmaxszOption ) );
            
            if ( 
equaliszName1 szName2 ) && ( szOption] != '0' ) )
            {
                
fputsiFile3 szLine1 );
                break;
            }
        }
        
        
fseekiFile2 SEEK_SET );
    }
    
    
fcloseiFile1 );
    
fcloseiFile2 );
    
fcloseiFile3 );
    
    
delete_fileszFile1 );
    
rename_fileszFile3 szFile1 );

Thank you very much ^^ that helped me a lot
__________________


D4rkSiD3Rs is offline