SA-MP Forums Archive
Extracting strings from a file. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: Extracting strings from a file. (/showthread.php?tid=211021)



Extracting strings from a file. - Whizion - 14.01.2011

So recently because of my scripting error some files were written wrong and now i want to fix that.

Here's an example of my file (it doesn't look like this, it's just a example):

Quote:

947tz47gz47zh7gz897ghguiohubhjgbejjcujeuefguef7gf7 3tg7tg37gf73gf73g7gg
rgz3grf783gf783gh7f3ghgh378gh3gh3g3hg8h3g783hg783h g3
3g3hh37hg73hg73hg7h37gh3gh3hg73hg7h37h37hg73hg73hg 7h37gh37gh73hg73hg
g3g33gg 378h373h7hg7gh73gh7g3h73gh3g7h3g3
3g3gg3hg3hg73hg7h3g7h37gh37h3g73gh73g7"user-someusername"h7fg3g73ghf73g73gb73g37gh3g7
3g3hh37hg73hg73hg7h37gh3gh3hg73hg7h37h37hg73hg73hg 7h37gh37gh73hg73hg
g3g33gg 378h373h7hg7gh73gh7g3h73gh3g7h3g3
3g3gg3hg3hg73hg7h3g7h37gh37h3g73gh73g7"user-someusername"h7fg3g73ghf73g73gb73g37gh3g7
rgz3grf783gf783gh7f3ghgh378gh3gh3g3hg8h3g783hg783h g3
3g3hh37hg73hg73hg7h37gh3gh3hg73hg7h37h37hg73hg73hg 7h37gh37gh73hg73hg
g3g33gg 378h373h7hg7gh73gh7g3h73gh3g7h3g3

So what i want to do is, to extract all the red parts in the file (there's over 500, so it's hard to do it manualy).

What i know is this, every part that i need to extract starts with "user- and the rest is the username ended by a ".

I tryed something with strcmp, strfind etc. but i failed.

Could somebody please write me some code that could do this? Thank you.


Re: Extracting strings from a file. - Babul - 14.01.2011

hm.. could you please upload 2 or 3 complete files to solidfiles? i want to have a look on them with an ascii editor... i hope to find some 0x0a chars, those are returns, i will try to sscanf for "%d%z" and compare if the %d is a number followed by one of those 0x0a. if the remaining %z string is not numeric, it can be saved to another file... well, iam ready to roll ^^


Re: Extracting strings from a file. - Finn - 14.01.2011

Quote:
Originally Posted by Babul
Посмотреть сообщение
hm.. could you please upload 2 or 3 complete files to solidfiles? i want to have a look on them with an ascii editor... i hope to find some 0x0a chars, those are returns, i will try to sscanf for "%d%z" and compare if the %d is a number followed by one of those 0x0a. if the remaining %z string is not numeric, it can be saved to another file... well, iam ready to roll ^^
What the hell.


Re: Extracting strings from a file. - Whizion - 14.01.2011

Whoa, thanks for the quick response.

Well it's not the same but i recreated it using dummy text: http://solidfiles.com/d/3956f/

One thing is certain it begins with "user-HEREGOESUSERNAME and ends with ".

Thanks a lot.


Re: Extracting strings from a file. - Finn - 14.01.2011

If the username is between " 's, what's the problem here?

pawn Код:
new File:example = fopen("lol.txt", io_read);
   
new string[256], name[64];
while(fread(example, string))
{
    new i, start = -1;
    while(string[i])
    {
        if(string[i] == '"')
        {
            if(start != -1)
            {
                strmid(name, string, start, i);
                break;
            }
            else
            {
                start = (i + 1);
            }
        }
        i++;
    }
       
    print(string);
    print(name);
}
   
fclose(example);
Edit: If you don't want the 'user-' to be included, simply change this part:

pawn Код:
start = (i + 1);
to this:
pawn Код:
start = (i + 6);



Re: Extracting strings from a file. - Whizion - 14.01.2011

There might be more " 's than those associated with the user-, will this code work for that to?

Thanks.


Re: Extracting strings from a file. - Finn - 14.01.2011

Quote:
Originally Posted by Whizion
Посмотреть сообщение
There might be more " 's than those associated with the user-, will this code work for that to?

Thanks.
No, it doesn't.

Your example text didn't have " 's elsewhere, so I thought that's how it is, sorry 'bout that.


Re: Extracting strings from a file. - Babul - 14.01.2011

@ Whizion: well, nvm. i guess Finn will do that for ya by reverting your file-write-routine. if the whole user file looks like your fake one, then you could create a macro using a tool like autohotkey, and find each "user- ,mark some chars (24 maybe?), copy them, paste them to another window, and repeat.
@ Finn. "what the hell" ? what do you mean? i guess youre smart enough to get what i asked for (despite my horrible english), so whats the prob? are you afraid iam stealing his userdata? or you got a problem that iam analyzing his little fakeuserdata.txt with another program to cut out a simple string after a "user- like its possible with EXIF informations in any picture?

btw in the whole time we spent arguing about this little shit, one of us could have fixed that by doing it in notepad, unless you already got 100k players ^^