AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   File line replacing (https://forums.alliedmods.net/showthread.php?t=135881)

tm. 08-20-2010 13:47

File line replacing
 
Hey. Let's say you have a text file which contains this data:
Code:

Player1 somedata moredata
Player2 somedata moredata
Player3 somedata moredata
Player4 somedata moredata

How to modify the file to replace
Code:

Player1 somedata moredata
with
Code:

Player999 otherdata moredata
while keeping the rest the same. Basically:
Code:

Player999 otherdata moredata
Player2 somedata moredata
Player3 somedata moredata
Player4 somedata moredata


katna 08-20-2010 14:55

Re: File line replacing
 
Well i just maked some test today and you can take a look at it.
PHP Code:

/* Plugin generated by AMXX-Studio */
#include <amxmodx>
#include <amxmisc>
#define PLUGIN "Test Plugin"
#define VERSION "1.0"
#define AUTHOR "death"
new gFile[64]
public 
plugin_init() {
 
register_plugin(PLUGINVERSIONAUTHOR)
 
register_clcmd("say /print""PrintFile")
 
register_clcmd("say /create""CreateFile")
 
register_clcmd("say /update""UpdateFile")
}
public 
CreateFile(id)
{
 new 
dir[64];
 
get_datadir(dircharsmax(dir))
 
 new 
folder[64];
 
formatex(foldercharsmax(folder), "/bmstats")
 
 
add(dircharsmax(dir), folder)
 if ( !
dir_exists(dir) ) mkdir(dir)
 
 
formatex(gFilecharsmax(gFile), "%s/bmstats.bm"dir)
 
 new 
data[128]
 new 
file;
 
 
file fopen(gFile"wt");
 
 for(new 
010;i++)
 {
  
formatex(datacharsmax(data), "^"Name %d^" ^"%s^" ^"^" ^"^"^n"i"test")
  
fputs(filedata);
 }
 
client_print(idprint_chat"good create")
 
 
fclose(file);
}
public 
PrintFile(id)
{
 new 
file
 
new data[128]
 
file fopen(gFile"rt")
 
 while ( !
feof(file) )
 {
  
fgets(filedatacharsmax(data))
  
client_print(idprint_console"%s"data)
 }
 
 
client_print(idprint_chat"good read")
 
fclose(file);
}
public 
UpdateFile(id)
{
 new 
file
 
new data[128]
 
file fopen(gFile"rt")
 new 
count 0
 
while ( !feof(file) )
 {
  
fgets(filedatacharsmax(data))
  if(
count == 2)
   
write_file(gFile"Edited Line 2"count)
  
count++
 }
 
 
client_print(idprint_chat"good update")
 
 
fclose(file);


you just need to count the line and the use
PHP Code:

write_file(const file[], const text[], line = -1


tm. 08-20-2010 15:45

Re: File line replacing
 
Thanks. I'm an idiot, I really should read more thoroughly those includes.


All times are GMT -4. The time now is 22:01.

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