Raised This Month: $12 Target: $400
 3% 

Solved Modify a file using another file


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
D4rkSiD3Rs
Senior Member
Join Date: Jan 2018
Location: Morocco
Old 10-09-2018 , 14:48   Modify a file using another file
Reply With Quote #1

Hello,

I made that plugin which gets the names from a file 'file1.ini' and check if they are on another file 'file2.ini', and if the second argument in the line which the name is found (on file2) is 0 then delete that line in the file1. But, it didn't work! Can someone see where is the problem?

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

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

public 
CleanFile()
{
    
/* File 1 */
    
new configdir[200];
    
get_configsdir(configdir199);

    
format(configdir199"%s/file1.ini"configdir);

    new 
line 0;
    new 
linetextlength 0;
    new 
linetext[512]

    new 
name[32], option[32];
    
/* ****** */


    /* File 2 */
    
new configdir2[200];
    
get_configsdir(configdir2199);

    
format(configdir2199"%s/file2.ini"configdir2);

    new 
line2 0;
    new 
linetextlength2 0;
    new 
linetext2[512]

    new 
name2[32];
    
/* ****** */


    
while(read_file(configdirline++, linetextcharsmax(linetext), linetextlength))
    {
        
parse(linetextnamecharsmax(name), optioncharsmax(option));

        while(
read_file(configdir2line2++, linetext2charsmax(linetext2), linetextlength2))
        {
            
parse(linetext2name2charsmax(name2));

            if( 
equali(name2name) && equali(option"0") )
            {
                
formatex(linetextcharsmax(linetext), "");
                
write_file(configdirlinetextline 1);
            }
        }
    }

__________________



Last edited by D4rkSiD3Rs; 10-13-2018 at 17:52.
D4rkSiD3Rs is offline
Kushfield
Member
Join Date: Jan 2017
Location: Estonia
Old 10-09-2018 , 17:27   Re: Modify a file using another file
Reply With Quote #2

I'm not sure if that's the only issue, but according to your description, the "option" part should be in file2. However, you try to get it from file1, so:
PHP Code:
parse(linetextnamecharsmax(name), optioncharsmax(option));
...
parse(linetext2name2charsmax(name2)); 
-->
PHP Code:
parse(linetextnamecharsmax(name));
...
parse(linetext2name2charsmax(name2), optioncharsmax(option)); 
If that's not the problem or doesn't fix it, I'd also request that you post some examples of the records in both of your files so we can better understand what you're working with.
Kushfield is offline
D4rkSiD3Rs
Senior Member
Join Date: Jan 2018
Location: Morocco
Old 10-10-2018 , 05:56   Re: Modify a file using another file
Reply With Quote #3

Quote:
Originally Posted by Kushfield View Post
I'm not sure if that's the only issue, but according to your description, the "option" part should be in file2. However, you try to get it from file1, so:
PHP Code:
parse(linetextnamecharsmax(name), optioncharsmax(option));
...
parse(linetext2name2charsmax(name2)); 
-->
PHP Code:
parse(linetextnamecharsmax(name));
...
parse(linetext2name2charsmax(name2), optioncharsmax(option)); 
If that's not the problem or doesn't fix it, I'd also request that you post some examples of the records in both of your files so we can better understand what you're working with.
Lol, thank you I didn't notice that, but it doesn't fix it
__________________


D4rkSiD3Rs is offline
D4rkSiD3Rs
Senior Member
Join Date: Jan 2018
Location: Morocco
Old 10-10-2018 , 15:40   Re: Modify a file using another file
Reply With Quote #4

any help ?
__________________


D4rkSiD3Rs is offline
JocAnis
Veteran Member
Join Date: Jun 2010
Old 10-10-2018 , 15:53   Re: Modify a file using another file
Reply With Quote #5

just put debug line//console print// per line, and see to where your code is 'executed' ?
__________________
KZ Public Autocup - PrimeKZ

My blog: http://primekz.xyz (in progress...) - not active (dec 2022)
JocAnis is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 10-10-2018 , 18:08   Re: Modify a file using another file
Reply With Quote #6

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 );

__________________

Last edited by Bugsy; 10-10-2018 at 18:47.
Bugsy is offline
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
D4rkSiD3Rs
Senior Member
Join Date: Jan 2018
Location: Morocco
Old 10-11-2018 , 15:11   Re: Modify a file using another file
Reply With Quote #8

Quote:
Originally Posted by Bugsy View Post
the deletion of the old file1 and renaming of temp to file1 fails.
No problem, anyway i will use that plugin once a month x) so I can rename the file myself.
__________________


D4rkSiD3Rs is offline
Reply


Thread Tools
Display Modes

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 04:13.


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