C split a char array into different variables

后端 未结 7 1095
不思量自难忘°
不思量自难忘° 2020-12-03 08:31

In C how can I separate a char array by a delimiter? Or is it better to manipulate a string? What are some good C char manipulation functions?

7条回答
  •  余生分开走
    2020-12-03 09:03

    This is how I do it.

    void SplitBufferToArray(char *buffer, char * delim, char ** Output) {
    
        int partcount = 0;
        Output[partcount++] = buffer;
    
        char* ptr = buffer;
        while (ptr != 0) { //check if the string is over
            ptr = strstr(ptr, delim);
            if (ptr != NULL) {
                *ptr = 0;
                Output[partcount++] = ptr + strlen(delim);
                ptr = ptr + strlen(delim);
            }
    
        }
        Output[partcount++] = NULL;
    }
    

提交回复
热议问题