AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved Modify a file using another file (https://forums.alliedmods.net/showthread.php?t=311260)

D4rkSiD3Rs 10-09-2018 14:48

Modify a file using another file
 
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);
            }
        }
    }



Kushfield 10-09-2018 17:27

Re: Modify a file using another file
 
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.

D4rkSiD3Rs 10-10-2018 05:56

Re: Modify a file using another file
 
Quote:

Originally Posted by Kushfield (Post 2619092)
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 10-10-2018 15:40

Re: Modify a file using another file
 
any help ?

JocAnis 10-10-2018 15:53

Re: Modify a file using another file
 
just put debug line//console print// per line, and see to where your code is 'executed' ?

Bugsy 10-10-2018 18:08

Re: Modify a file using another file
 
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 );



D4rkSiD3Rs 10-11-2018 15:06

Re: Modify a file using another file
 
Quote:

Originally Posted by Bugsy (Post 2619212)
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 10-11-2018 15:11

Re: Modify a file using another file
 
Quote:

Originally Posted by Bugsy (Post 2619212)
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.


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

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