Raised This Month: $ Target: $400
 0% 

Replacing file


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
micke1101
Veteran Member
Join Date: Jan 2008
Location: Banned-town
Old 07-14-2009 , 16:27   Replacing file
Reply With Quote #1

Whats the best way of replacing a file with another?

Code example is appreciated

Thanks in advanced!
micke1101 is offline
johnjg75
Veteran Member
Join Date: Mar 2004
Location: Delaware
Old 07-14-2009 , 16:45   Re: Replacing file
Reply With Quote #2

i would assume delete_file() http://www.amxmodx.org/funcwiki.php?go=func&id=89
then write_file() http://www.amxmodx.org/funcwiki.php?go=func&id=88
__________________
johnjg75 is offline
Send a message via AIM to johnjg75 Send a message via MSN to johnjg75 Send a message via Yahoo to johnjg75
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 07-14-2009 , 17:17   Re: Replacing file
Reply With Quote #3

You can delete the file being replaced, then rename the file that is replacing it.

OR

You can delete the file being replaced, and copy the contents of the replacing file into a new file with the name of the file being replaced.

Code:
// method 1 ReplaceFile(const szFileReplacing[], const szFileReplaced[]) {     delete_file(szFileReplaced);         while( !rename_file(szFileReplacing, szFileReplaced, 1) ) { } } // method 2 ReplaceFile(const szFileReplacing[], const szFileReplaced[]) {     new iFileReplaced = fopen(szFileReplaced, "wt");     new iFileReplacing = fopen(szFileReplacing, "rt");         static szData[1024];     while( !feof(iFileReplacing) )     {         fgets(iFileReplacing, szData, 1023);         fputs(iFileReplaced, szData);     }         fclose(iFileReplaced);     fclose(iFileReplacing); }
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
micke1101
Veteran Member
Join Date: Jan 2008
Location: Banned-town
Old 07-15-2009 , 02:11   Re: Replacing file
Reply With Quote #4

Thank you

But im not so ised to this so of course it wont compile and i cant get why (though i will continue search)
PHP Code:
#include <amxmodx>

new motdyearmotdmonthmotdday;

new 
FileToReplaceWith;

public 
plugin_init(){
    
register_plugin("Date based motd""0.1""Micke1101")

    
register_cvar("motd_check""2"//When to check the date 1 = at start 2 = end 3 = end and start
}
public 
round_start(){
    if(
get_cvar_num("motd_check") == || get_cvar_num("motd_check") == 3){
        
date(motdyearmotdmonthmotdday)
        if(
file_exists("motds/&s%s%s.txt",motdyearmotdmonth,motdday)){
            
FileToReplaceWith "motds/&s%s%s.txt",motdyear ,motdmonth ,motdday;
        } else {
            
FileToReplaceWith "motds/default.txt";
        }
        
ReplaceFile();
    }
}
public 
round_end(get_cvar_num("sv_roundtime")){
    if(
get_cvar_num("motd_check") == || get_cvar_num("motd_check") == 3){
        
date(motdyearmotdmonthmotdday)
        if(
file_exists("motds/&s%s%s.txt",motdyear ,motdmonth ,motdday)){
            
FileToReplaceWith "motds/&s%s%s.txt",motdyear ,motdmonth ,motdday;
        } else {
            
FileToReplaceWith "motds/default.txt";
        }
        
ReplaceFile();
    }
}
ReplaceFile(const szFileReplacing[], const szFileReplaced[])
{
    new 
iFileReplaced fopen(szFileReplaced"motd.txt");
    new 
iFileReplacing fopen(szFileReplacing"%s"FileToReplaceWith);

    static 
szData[1024];
    while( !
feof(iFileReplacing) )
    {
        
fgets(iFileReplacingszData1023);
        
fputs(iFileReplacedszData);
    }

    
fclose(iFileReplaced);
    
fclose(iFileReplacing);

micke1101 is offline
johnjg75
Veteran Member
Join Date: Mar 2004
Location: Delaware
Old 07-15-2009 , 02:24   Re: Replacing file
Reply With Quote #5

FileToReplaceWith should be an array.
And you didn't pass any parameters to ReplaceFile(), but it looks like you changed it so just erase the parameters to look like this
PHP Code:
ReplaceFile()

__________________

Last edited by johnjg75; 07-15-2009 at 02:33.
johnjg75 is offline
Send a message via AIM to johnjg75 Send a message via MSN to johnjg75 Send a message via Yahoo to johnjg75
micke1101
Veteran Member
Join Date: Jan 2008
Location: Banned-town
Old 07-15-2009 , 13:51   Re: Replacing file
Reply With Quote #6

I thought it was a array
Code:
new FileToReplaceWith
micke1101 is offline
platzpatrone
Veteran Member
Join Date: Apr 2007
Location: Germany
Old 07-15-2009 , 14:02   Re: Replacing file
Reply With Quote #7

Quote:
Originally Posted by micke1101 View Post
I thought it was a array
Code:
new FileToReplaceWith
shouldnt it be like this:

Code:
new FileToReplaceWith[32]
to be an array ?
platzpatrone is offline
Owyn
Veteran Member
Join Date: Nov 2007
Old 07-15-2009 , 14:52   Re: Replacing file
Reply With Quote #8

PHP Code:
new FileToReplaceWith[32][32
to have a 32 demension array with max length of an item of 32 char
__________________
☜ Free Mozy ☂backup\҉sync user
Quote:
Американский форум - Задаёшь вопрос, потом тебе отвечают.
Израильский форум - Задаёшь вопрос, потом тебе задают вопрос.
Русский форум - Задаёшь вопрос, потом тебе долго рассказывают, какой ты мудак.
Owyn is offline
Send a message via ICQ to Owyn
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 07-15-2009 , 15:52   Re: Replacing file
Reply With Quote #9

1. Pawn isn't a language that you can easily format variables like that.
You have to call specific functions to format the variable.

2. You should also use pcvars.

3. You could also reduce the redundant code.

4. You never registered the round_start and round_end events.

5. Instead of getting the cvar value twice, only get it once.

6. Don't modify the ReplaceFile() function.

Code:
#include <amxmodx> new FileToReplaceWith[64]; new cvar_check; public plugin_init(){     register_plugin("Date based motd", "0.1", "Micke1101")         register_logevent("round_start", 2, "1=Round_Start");     register_logevent("round_end", 2, "1=Round_End");     cvar_check = register_cvar("motd_check", "2") //When to check the date 1 = at start 2 = end 3 = end and start } public round_start(){     new check = get_pcvar_num(cvar_check);     if(check == 1 || check == 3){         DoMotdFile();     } } public round_end(){     new check = get_pcvar_num(cvar_check);     if(check == 2 || check == 3){         DoMotdFile();     } } DoMotdFile() {     new motdyear, motdmonth, motdday;     date(motdyear, motdmonth, motdday)     formatex(FileToReplaceWith, 63, "motds/%i%i%i.txt", motdyear, motdmonth, motdday);     if(!file_exists(FileToReplaceWith)){         copy(FileToReplaceWith, 63, "motds/default.txt");     }     ReplaceFile(FileToReplaceWith, "motd.txt"); } ReplaceFile(const szFileReplacing[], const szFileReplaced[]) {     if( !file_exists(szFileReplacing) ) return;         new iFileReplaced = fopen(szFileReplaced, "wt");     new iFileReplacing = fopen(szFileReplacing, "rt");     static szData[1024];     while( !feof(iFileReplacing) )     {         fgets(iFileReplacing, szData, 1023);         fputs(iFileReplaced, szData);     }     fclose(iFileReplaced);     fclose(iFileReplacing); }
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
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 13:11.


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