strcmp with sscanf
#1

Can i use sscanf for basic strcmp commands?

I mean, if(sscanf(cmdtext...instead of if(sscanf(params...?

Or any way for it?



Sorry for bad english.
Reply
#2

At the top of your script:

pawn Code:
#include <sscanf2>
This is OnPlayerCommandText:

pawn Code:
public OnPlayerCommandText(playerid, cmdtext[])
{
    if (strcmp("/test", cmdtext, true, 10) == 0)
    {
        new id;
        if(sscanf(cmdtext, "i", id)) return 1;
        {
            return 1;
        }
    }
    if(strcmp("/nextcommand", cmdtext, true, 10) == 0)
    {
        return 1;
    }
    return 1;
}
Untested.
Reply
#3

Quote:
Originally Posted by iGetty
View Post
At the top of your script:

pawn Code:
#include <sscanf2>
This is OnPlayerCommandText:

pawn Code:
public OnPlayerCommandText(playerid, cmdtext[])
{
    if (strcmp("/test", cmdtext, true, 10) == 0)
    {
        new id;
        if(sscanf(cmdtext, "i", id)) return 1;
        {
            return 1;
        }
    }
    if(strcmp("/nextcommand", cmdtext, true, 10) == 0)
    {
        return 1;
    }
    return 1;
}
Untested.
If your code isnt tested, you shouldnt post it. Those commands wont work correctly! Not only are specifying the wrong length to the strcmp function (10 for both, which neither equal 10!), but your passing the entire cmdtext string to sscanf, which includes the original command.

pawn Code:
public OnPlayerCommandText(playerid, cmdtext[])
{
    if(!strcmp(cmdtext, "/Example", true, 8))
    {
        new
            number,
            string[16];

        if(!sscanf(cmdtext[9], "is[15]", number, string))
        {
            printf("number = %d, string = %s.", number, string);
            return 1;
        }
        SendClientMessage(playerid, 0xFF0000FF, "Syntax error: /Example <number> <string>");
        return 1;
    }

    return 0;
}
The above should work without any problems, but it's highly recommended to just use a command processor like zcmd/ycmd with sscanf. This not only is faster, but its much safer due to how easy it is to make mistakes using the above methodology.

Remember to always update the strcmp length parameter (should be the lenght of the command) and the index for cmdtext (should be the length of the command + 1) if you change the name of the command.
Reply
#4

Sorry Kyosaur, just trying to help.

No problem, at least I learnt something. :P
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)