open file and edit a string
#1

Hello,

I have a file with some names:

firstname_name1 firstname_name2 firstname_name3...

If I rename I want to edit my oldname (firstname_name2) by my new name (newname)

Код:
fopen("names.txt",io_readwrite);
   if (file)
    {
        while(fread(file,open,sizeof(open)))
        {
            if (strfind(open, playername, true) != -1)
            {
                    // if my oldname is on the list I want to edit this oldname by my newname
                    // so "firstname_name1 newname firstname_name3"
	            fclose(file);
	        }
regards.
Reply
#2

What do you mean? Can you rephrase what your actually trying to do? I can't understand you.
Reply
#3

ok I edit. If I rename I want to edit my oldname (firstname_name2) by my new name (newname)
Reply
#4

are the names on different line or same line with spaces?
Reply
#5

spaces yes
Reply
#6

here's a quick example.
pawn Код:
#define MAX_NAMES 74//decide for yourself. 24 * 3 + spaces (3 names)

new names[MAX_NAMES] = "name1 name2 name3";


main() {
    new newName[] = "newName";
    new oldName[] = "name1";
    new off;
    if((off = strfind(names,oldName))!=-1)
        strdel(names,off,off+strlen(oldName)),strins(names,newName,off);
    else printf("couldn't find '%s'",oldName);

    printf("names: %s",names)
}
/*
-Prints-
names: newName name2 name3

-changing 'oldName' to 'name3' it'll print-
names: name1 name2 newName
*/
"names" would be tha string in your file.
we first search for "oldName" in that string.
get the offset, delete everything from that offset to offset + lenght of "oldName".
Then insert our "newName" at that offset and thats it

you could copy & test it quickly at PPG

i hope this short example could help a little
Reply
#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
#8

Thanks but where is fwrite ? because the file it's not edited (oldname by newname)
Reply
#9

Oh sorry. Can you please post how your file looks?
I mean does it look like:
Код:
name1 name2 name3
or
Код:
name1 name2 name3
name4 name 5 name6
What I mean is, are they multiple lines?
Reply
#10

1 by line

Код:
Name1
Name2
Name3
...
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)