AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Help write file (https://forums.alliedmods.net/showthread.php?t=51047)

SAMURAI16 02-10-2007 09:50

Help write file
 
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

[ --<-@ ] Black Rose 02-10-2007 10:04

Re: Help write file
 
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) }

SAMURAI16 02-10-2007 10:16

Re: Help write file
 
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

[ --<-@ ] Black Rose 02-10-2007 10:44

Re: Help write file
 
Change "w" to "a".
Of course it only prints hello, you only read one arg. if you wanna read all use read_args()

SAMURAI16 02-10-2007 10:48

Re: Help write file
 
works thanks

XxAvalanchexX 02-10-2007 14:22

Re: Help write file
 
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.

[ --<-@ ] Black Rose 02-10-2007 16:07

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

Is it better to use fputc()?

XxAvalanchexX 02-10-2007 18:11

Re: Help write file
 
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.


All times are GMT -4. The time now is 00:38.

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