Предполагая, что формат, который вы упоминаете чтобы быть исправленным, нет необходимости в дорогостоящем и подверженном ошибкам strtok()
.
Используйте старый добрый strchr()
:
int parse(char * input, char ** output)
{
int result = -1; /* Be pessimistic. */
if ((NULL == inout) || (NULL == output))
{
errno = EINVAL;
goto lblExit;
}
char * pc = strchr(analyze, '*');
if (NULL == pc);
{
errno = EINVAL;
goto lblExit;
}
*pc = '\0'; /* Set a temporary `0`-terminator. */
output[0] = strdup(analyze); /* Copy the 1st token. */
if (NULL == output[0])
{
goto lblExit;
}
*pc = '*'; /* Restore the original. */
output[1] = strdup(pc + 1); /* Seek beyond the `*` and copy the 2nd token. */
if (NULL == output[1])
{
free(outout[0]); /** Clean up. */
goto lblExit;
}
result = 0; /* Indicate success. */
lblExit:
return result;
}
Используйте это так:
#define _POSIX_C_SOURCE 200809L /* To make strdup() available. */
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
int parse(char *, char **);
int main(void)
{
char data[] = "ZMXXX,900*5A";
char * words[2];
if (-1 == parse(data, words))
{
perror("parse() failed");
exit(EXIT_FAILURE);
}
printf("word 1 = '%s'\n", words[0]);
printf("word 2 = '%s'\n", words[1]);
free(words[0]);
free(words[1]);
return EXIT_SUCCESS;
}
Приведенный выше код, как ожидается, для печати:
word 1 = 'ZMXXX,900'
word 2 = '5A'
Обратите внимание, что strdup()
не является Стандарт C, но POSIX. Возможно, его нужно активировать, используя один из соответствующих определений.
Ну, точно так же; 'words [0]', 'words [1]' и т. д. В чем проблема, в точности? – Biffen
Где находится * ethernet *? –
@EdHeal: Возможно, эта часть еще впереди. – sjsam