Raised This Month: $ Target: $400
 0% 

Replacing line in file


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
djmd378
Senior Member
Join Date: Sep 2004
Old 05-12-2007 , 21:13   Replacing line in file
Reply With Quote #1

I'm fairly new to reading/writing to files so can someone show me how I would delete a line from a file then copy that same line but with additional information on it? Oh and I don't care if it leaves a blank space there.

Here's an example of what I want to do:

Original
PHP Code:
Line1 1
Line2 1
Line3 1 
New
PHP Code:
Line1 1
Line2 11
Line3 1 
I know I can do this by copying everything except the line I don't want into a new file then deleting the original and renaming the new file to the original. But the problem I had with this is that once I write additional information into it, the server crashes. So this is why I don't care for blank lines. =]

Thanks.
__________________
djmd378 is offline
scrtxxcaz
Senior Member
Join Date: Feb 2007
Location: a place u cant find
Old 05-13-2007 , 02:20   Re: Replacing line in file
Reply With Quote #2

heres an sorta of exmaple alter it to ur needs if it works for you unless some one else knows a better why of doing this.

Code:
 
public somin(id) {
          new a,b,c
          new filepointer = fopen(awpfile,"at+")
          if(filepointer)
          {
                 new readdata[128]
                 new parsed1[32],parsed2[32],parsed3[32]
                 while(fgets(filepointer,readdata,127))
                 {
                            parse(readdata,parsed1,31,parsed2,31,parsed3,31)
                            {
                                           if(contain(parsedauth,"watever") != -1)
                                           {
                                                     a = parsed1
                                                     b = parsed2
                                                     c = parsed3
                                           }
                             }
                   }
           }
           new text[512], line, len
           while ( read_file(file, line++, text, 511, len) ) 
           {
                     if ( contain(text, "whatever") != -1 ) 
                     {
                              new msg[256]
                              format(msg,255,"^"%s^" ^"%s^" ^"%s^" ",a,b,c) //change a,b, or c to the new info
                              replace(text, 511, text, msg)
                              write_file(file, text, line - 1)
                              return PLUGIN_HANDLED
 
                     }
          }
}
__________________
Please leave your name when giving me any karma

Counter Strike Freeze Tag
Status: Testing.... (bugs)

Revised Registration
Status: In Progress

Last edited by scrtxxcaz; 05-13-2007 at 02:25.
scrtxxcaz is offline
Send a message via AIM to scrtxxcaz
djmd378
Senior Member
Join Date: Sep 2004
Old 05-13-2007 , 13:00   Re: Replacing line in file
Reply With Quote #3

I get a runtime error when it tries to write to the file.

Code:
[AMXX] Run time error 10: native error (native "write_file")
[AMXX]    [0] test.sma::filereplacetest (line 66)
This is the line it is talking about
Code:
write_file(filename, readdata, line - 1)
Heres what the code looks like
Code:
public filereplacetest(id) {     new text[512], line, len     while(read_file(filename, line++, readdata, 511, len) )     {         if (contain(readdata, parsedsteamid) != -1 )         {             new msg[256]             format(msg, 255, "%s %s %s1", parsedsteamid, parsedname, parsednumber)             replace(readdata, 511, readdata, msg)             write_file(filename, readdata, line - 1)             return PLUGIN_HANDLED         }     }     return PLUGIN_HANDLED }
__________________
djmd378 is offline
scrtxxcaz
Senior Member
Join Date: Feb 2007
Location: a place u cant find
Old 05-14-2007 , 01:36   Re: Replacing line in file
Reply With Quote #4

do you have this or somin like this??
Code:
 
new filename[72]
 
public plugin_int() {
      get_datadir(filename,71)
      format(filename,71,"%/data/whatever.txt")
}
i might have forgotten to add that in the first post. if you dont have this in ur code then this could be the problem
__________________
Please leave your name when giving me any karma

Counter Strike Freeze Tag
Status: Testing.... (bugs)

Revised Registration
Status: In Progress
scrtxxcaz is offline
Send a message via AIM to scrtxxcaz
djmd378
Senior Member
Join Date: Sep 2004
Old 05-14-2007 , 01:56   Re: Replacing line in file
Reply With Quote #5

Quote:
Originally Posted by scrtxxcaz View Post
do you have this or somin like this??
i might have forgotten to add that in the first post. if you dont have this in ur code then this could be the problem

Something similar.

I have this.
Code:
new filename[256] public plugin_init() {  get_configsdir(filename,255)  format(filename,255,"%s/list.txt",filename) }
__________________
djmd378 is offline
scrtxxcaz
Senior Member
Join Date: Feb 2007
Location: a place u cant find
Old 05-14-2007 , 02:37   Re: Replacing line in file
Reply With Quote #6

I had this problem with one of my plugins before, but i forget excalty what it was. Can you paste your whole code, maybe it will refresh my memory. First though make sure you actually have a file called "list" in your configs folder.

you can also add this in your code if u dont have it:
Code:
new filename[256]
 
public plugin_init() {
      get_configsdir(filename,255)
      format(filename,255,"%s/list.txt",filename)
      if(!file_exists(filename))
      {
            write_file(filename,";list")
      }
}
__________________
Please leave your name when giving me any karma

Counter Strike Freeze Tag
Status: Testing.... (bugs)

Revised Registration
Status: In Progress

Last edited by scrtxxcaz; 05-14-2007 at 02:52.
scrtxxcaz is offline
Send a message via AIM to scrtxxcaz
djmd378
Senior Member
Join Date: Sep 2004
Old 05-14-2007 , 02:49   Re: Replacing line in file
Reply With Quote #7

I chopped off some stuff but this is basically it. Oh btw I do have a file called "list". I use the "write" command and it'll make it for me.

Code:
see bottem for more recent version
__________________

Last edited by djmd378; 05-14-2007 at 03:49.
djmd378 is offline
scrtxxcaz
Senior Member
Join Date: Feb 2007
Location: a place u cant find
Old 05-14-2007 , 02:58   Re: Replacing line in file
Reply With Quote #8

take out this part of your code in plugin_int;
Code:
 
delelte_file(filename)
when ever u start ur plugin and that is in there it will just delete the file and you will not be able to write to it. Try it after u delete that part.
__________________
Please leave your name when giving me any karma

Counter Strike Freeze Tag
Status: Testing.... (bugs)

Revised Registration
Status: In Progress
scrtxxcaz is offline
Send a message via AIM to scrtxxcaz
djmd378
Senior Member
Join Date: Sep 2004
Old 05-14-2007 , 03:00   Re: Replacing line in file
Reply With Quote #9

Oh I know, I want it to delete the file after every map load. Since I know it's doing that, everytime I test I always use the "write" command before the "replace" command.
__________________
djmd378 is offline
scrtxxcaz
Senior Member
Join Date: Feb 2007
Location: a place u cant find
Old 05-14-2007 , 03:18   Re: Replacing line in file
Reply With Quote #10

I had a problem were if there was something extra that was not used it created an error for my write_file and read_file i dont know why so check that. Any way i dont see a problem with your code but i could be wrong. im tired and a lil toasted so im out, ill look over it again tomor unless someone else ssoleves it.
__________________
Please leave your name when giving me any karma

Counter Strike Freeze Tag
Status: Testing.... (bugs)

Revised Registration
Status: In Progress
scrtxxcaz is offline
Send a message via AIM to scrtxxcaz
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 10:38.


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