Raised This Month: $ Target: $400
 0% 

Help write file


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
SAMURAI16
BANNED
Join Date: Sep 2006
Old 02-10-2007 , 09:50   Help write file
Reply With Quote #1

Hi, i need an example for this :
argv from console command to be wroted in a file
For example : i have a file, and if i use command : amx_wfile <text>, that "text" to be added in my file .
But on my file, i have some lines ; ex:
1) bla bla
2) hello
3) what you wany
I want that "text" to added under my lines from my file

Hope you are understand, thanks
SAMURAI16 is offline
Send a message via MSN to SAMURAI16
[ --<-@ ] Black Rose
ANNIHILATED
Join Date: Sep 2005
Location: Stockholm, Sweden.
Old 02-10-2007 , 10:04   Re: Help write file
Reply With Quote #2

Code:
new file[] = "..." public func() {     new text[32]     read_argv(1, text, 31)         new fileh = fopen(file, "w");         if ( ! fileh )         return PLUGIN_CONTINUE         fprintf(file2, "%s", text);         fclose(fileh); }

or simply
Code:
new file[] = "..." public func() {     new text[32]     read_argv(1, text, 31)         write_file(file, text) }
[ --<-@ ] Black Rose is offline
SAMURAI16
BANNED
Join Date: Sep 2006
Old 02-10-2007 , 10:16   Re: Help write file
Reply With Quote #3

ok, this is public
Code:
public add_rule(id,level,cid)
{
    if( ! cmd_access ( id , level, cid , 2) )
    return PLUGIN_HANDLED;
    
    new g_FILE[128]
    get_localinfo("amxx_configsdir",g_FILE,127)
    format(g_FILE,127,"%s/sayrules.txt",g_FILE)
    
    new text[32]
    read_argv(1,text,31)
    
    new fileh = fopen(g_FILE, "w");
    
    if ( ! fileh )
    return PLUGIN_CONTINUE
    
    fprintf(fileh, "%s", text);
    
    fclose(fileh);
    
    return PLUGIN_HANDLED;
}
Sure, i register that public, with comand amx_addrule ;
So, i used command : amx_addrulle hello all
After i use command:

1) Cleared all information from my file
2) Added only "hello" on file
SAMURAI16 is offline
Send a message via MSN to SAMURAI16
[ --<-@ ] Black Rose
ANNIHILATED
Join Date: Sep 2005
Location: Stockholm, Sweden.
Old 02-10-2007 , 10:44   Re: Help write file
Reply With Quote #4

Change "w" to "a".
Of course it only prints hello, you only read one arg. if you wanna read all use read_args()
[ --<-@ ] Black Rose is offline
SAMURAI16
BANNED
Join Date: Sep 2006
Old 02-10-2007 , 10:48   Re: Help write file
Reply With Quote #5

works thanks
SAMURAI16 is offline
Send a message via MSN to SAMURAI16
XxAvalanchexX
Veteran Member
Join Date: Oct 2004
Location: abort73.com
Old 02-10-2007 , 14:22   Re: Help write file
Reply With Quote #6

After
Code:
fprintf(fileh, "%s", text);
you will probably want to add
Code:
fputc(fileh, '^n'); // ^n is the newline character
This way, the next time you write to the file, it will be on the next line down, instead of added to the last line.
__________________
No longer around. Thanks your support, everyone! As always:
THIS ONES FOR YOU
3000 PTS
XxAvalanchexX is offline
[ --<-@ ] Black Rose
ANNIHILATED
Join Date: Sep 2005
Location: Stockholm, Sweden.
Old 02-10-2007 , 16:07   Re: Help write file
Reply With Quote #7

Code:
fprintf(fileh, "%s^n", text);
Easier.

Is it better to use fputc()?
[ --<-@ ] Black Rose is offline
XxAvalanchexX
Veteran Member
Join Date: Oct 2004
Location: abort73.com
Old 02-10-2007 , 18:11   Re: Help write file
Reply With Quote #8

I read somewhere that you shouldn't put the newline character in fprintf, because it will just be written out as ^n, instead of actually making a newline. I don't know its validity, though, it was in reference to C and not Pawn.
__________________
No longer around. Thanks your support, everyone! As always:
THIS ONES FOR YOU
3000 PTS
XxAvalanchexX 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 00:38.


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