用strtok_r分割可变数量的标记

时间:2010-11-30 12:44:54

标签: c string

我通过以下格式命名管道命令:

GETIP <machine_name> \n
<process_id>

GETNAME<ip_address> \n
<process_id>

UDATA <machine_name> <ip_address>
<process_id>

DDATA <machine_name> \n
<process_id>

因此,从管道读取的样本字符串是:

GETIP lolcatzmachine
1235

UDATA cheezburger 127.0.0.1
7564

说实话,我不知道我到底在做什么,我不熟悉c标记。如何更改代码以满足要求?

char *token; 

char *commandName [10];
char machineName[200];
char ip[40];
char pid[30];

char * separator = "  "; 
char *brkt; // reentrant pointer, as this tokenizing will be multithreaded

for ( ; ; ) {

 token=strtok_r(command_and_pid, separator, &brkt); //strtok_r is needed for multithreading

 commandName = strdup(token);

    //ip=strtok_r(NULL, separator, &brkt);

   //pid=strtok_r(NULL, separator, &brkt);

  if (token == NULL)
        break;
}

2 个答案:

答案 0 :(得分:0)

看看这个。注意:没有测试过&amp;优化

    #include <stdio.h>
#include <string.h>
#include <malloc.h>

#include "strtok.h"

//char temp[100];

char *strTok(char *string, char *delimiter)
{
    static char *inputStr = NULL;
    char *temp = NULL;
    int i,count,j;

    if (string != NULL)
    {

        if( inputStr != NULL)
            free(inputStr);     

        inputStr = (char*) malloc (sizeof(char) * strlen(string));
        strcpy(inputStr,string);

    }

    for( i = 0 ; i < strlen(inputStr) ; i++ )
    {
        if ( inputStr[i] == delimiter[0] )
        {
            count = 1;
            for( j = 1 ; j < strlen(delimiter) ; j++ )
            {
                if ( inputStr[i+j] == delimiter[j] )
                    count ++;
                else
                    break;
            }           
        }   

        if ( count == strlen(delimiter) )
        {

            if( temp != NULL)
                free(temp);     


            temp = (char*) malloc (sizeof(char) * i);   

            strncpy(temp,inputStr,i);
            strcpy(inputStr,inputStr+i+strlen(delimiter));

            return temp;

        }


    }

    return inputStr;
}

/*int main()
{
    char *str = strTok("title:4:int",":");
    printf("%s\n",str);

    str = strTok(NULL,":");
    printf("%s\n",str);

    str = strTok(NULL,":");
    printf("%s\n",str);

    str = strTok(NULL,":");
    printf("%s\n",str);
    str = strTok("director:int:sdjjsd",":");
    printf("%s\n",str);

    str = strTok(NULL,":");
    printf("%s\n",str);


}*/

答案 1 :(得分:0)

我最终使用了这个的修改版本:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int
main(int argc, char *argv[])
{
    char *str1, *str2, *token, *subtoken;
    char *saveptr1, *saveptr2;
    int j;

    if (argc != 4) {
        fprintf(stderr, "Usage: %s string delim subdelim\n",
                argv[0]);
        exit(EXIT_FAILURE);
    }

    for (j = 1, str1 = argv[1]; ; j++, str1 = NULL) {

       token = strtok_r(str1, argv[2], &saveptr1);

       if (token == NULL)
            break;
        printf("%d: %s\n", j, token);

        for (str2 = token; ; str2 = NULL) {
            subtoken = strtok_r(str2, argv[3], &saveptr2);
            if (subtoken == NULL)
                break;
            printf("     --> %s\n", subtoken);
        }
    }

    exit(EXIT_SUCCESS);
} /* main */