I am trying to create an application that responds to multiple commands inputted by the users. The problem is that these commands have multiple parameters. Some examples would be
login 'username' 'password'
add song 'song_name' 'description' 'genre' 'link'
The input is read into a char array.
char recv[max]=" ";
if (read (fd, recv, sizeof (recv)) < 0)
{
perror ("[server]Error on read () from client.\n");
return errno;
}
This is all part of a bigger uni project using servers, so that is the reason for using read(). max is defined as 1024.
While I can use strstr() to identify which command is which, I'm not sure how I could get the username, password and so on as separate substrings.
I know that I can get rid of the command part by copying into another char the input+the command length, as it is fixed, but I'm keen on finding out how I can select each substring between two ''s. Would strtok() do the job?
Thanks in advance.
"would formatting input in a different manner make it any easier?".
Eg, given the following multi delimiter string :
char str[] = {"this,is|a:multi.delimited$string"};
char *dup = strdup(str);
if(dup)
char *tok = strtok(dup,",");
if(tok)
{
printf("%s\n", tok);
tok = strtok(NULL, "|");
if(tok)
{
printf("%s\n", tok);
tok = strtok(NULL, ":");
if(tok)
{
//and so on through each unique delimiter
Illustrates how string parsing with different delimiters can be simply done using strtok().