AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Replacing line in file (https://forums.alliedmods.net/showthread.php?t=55082)

djmd378 05-12-2007 21:13

Replacing line in file
 
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.

scrtxxcaz 05-13-2007 02:20

Re: Replacing line in file
 
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
 
                    }
          }
}


djmd378 05-13-2007 13:00

Re: Replacing line in file
 
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 }

scrtxxcaz 05-14-2007 01:36

Re: Replacing line in file
 
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

djmd378 05-14-2007 01:56

Re: Replacing line in file
 
Quote:

Originally Posted by scrtxxcaz (Post 476417)
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) }

scrtxxcaz 05-14-2007 02:37

Re: Replacing line in file
 
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")
      }
}


djmd378 05-14-2007 02:49

Re: Replacing line in file
 
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

scrtxxcaz 05-14-2007 02:58

Re: Replacing line in file
 
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.

djmd378 05-14-2007 03:00

Re: Replacing line in file
 
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.

scrtxxcaz 05-14-2007 03:18

Re: Replacing line in file
 
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.


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

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