Raised This Month: $ Target: $400
 0% 

File line replacing


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
tm.
Member
Join Date: Apr 2010
Old 08-20-2010 , 13:47   File line replacing
Reply With Quote #1

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
tm. is offline
katna
Senior Member
Join Date: May 2010
Old 08-20-2010 , 14:55   Re: File line replacing
Reply With Quote #2

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
katna is offline
tm.
Member
Join Date: Apr 2010
Old 08-20-2010 , 15:45   Re: File line replacing
Reply With Quote #3

Thanks. I'm an idiot, I really should read more thoroughly those includes.

Last edited by tm.; 08-20-2010 at 15:47.
tm. 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 21:09.


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