open file and edit a string
#7

pawn Код:
new File:file = fopen("names.txt",io_readwrite);
if (file)
{
    new readstr[200], pos;
    while(fread(file, readstr))
    {
        pos = strfind(readstr, playername, true);
        if (pos != -1)
        {
            strreplace(readstr, playername, "Your new name here", true);
            fclose(file);
        }
    }
}
You need this function (by Slice)
pawn Код:
stock strreplace(string[], const search[], const replacement[], bool:ignorecase = false, pos = 0, limit = -1, maxlength = sizeof(string)){
    // No need to do anything if the limit is 0.
    if (limit == 0) return 0;
    new
             sublen = strlen(search),
             replen = strlen(replacement),
        bool:packed = ispacked(string),
             maxlen = maxlength,
             len = strlen(string),
             count = 0
    ;
    // "maxlen" holds the max string length (not to be confused with "maxlength", which holds the max. array size).
    // Since packed strings hold 4 characters per array slot, we multiply "maxlen" by 4.
    if (packed) maxlen *= 4;

    // If the length of the substring is 0, we have nothing to look for..
    if (!sublen) return 0;

    // In this line we both assign the return value from "strfind" to "pos" then check if it's -1.
    while (-1 != (pos = strfind(string, search, ignorecase, pos))) {
        // Delete the string we found
        strdel(string, pos, pos + sublen);

        len -= sublen;

        // If there's anything to put as replacement, insert it. Make sure there's enough room first.
        if (replen && len + replen < maxlen) {
            strins(string, replacement, pos, maxlength);

            pos += replen;
            len += replen;
        }

        // Is there a limit of number of replacements, if so, did we break it?
        if (limit != -1 && ++count >= limit)
            break;
    }
    return count;
}
Reply


Messages In This Thread
open file and edit a string - by ombre - 30.11.2014, 01:48
Re: open file and edit a string - by Abagail - 30.11.2014, 01:50
Re : open file and edit a string - by ombre - 30.11.2014, 10:21
Re: open file and edit a string - by BroZeus - 30.11.2014, 10:30
Re : open file and edit a string - by ombre - 30.11.2014, 10:38
Re: open file and edit a string - by CutX - 30.11.2014, 11:12
Re: open file and edit a string - by Stinged - 30.11.2014, 13:19
Re : open file and edit a string - by ombre - 02.12.2014, 01:21
Re: open file and edit a string - by Stinged - 02.12.2014, 16:19
Re : open file and edit a string - by ombre - 02.12.2014, 16:43

Forum Jump:


Users browsing this thread: 1 Guest(s)