реклама на сайте
подробности

 
 
> C18 Функция strtok
Auratos
сообщение Oct 29 2015, 13:53
Сообщение #1


Участник
*

Группа: Участник
Сообщений: 26
Регистрация: 6-10-14
Пользователь №: 83 032



Добрый день. У меня возникла необходимость парсить большое количество строк и вычленять числа из строки, разделенные между собой точкой (например, "15.23.10"). Стал копать в эту сторону и набрел на функцию strtok, которая призвана разделять строку на подстроки, разделенные маркером, передаваемым в функцию в виде параметра. Нашел готовый пример
Код
#include <string.h>

int main()
{
    char str[] = "192.168.0.1";
    unsigned char bytes[4];
    int i = 0;

    char* buff = malloc(10);
    buff = strtok(str,".");
    while (buff != NULL)
    {
       //if you want to print its value
       printf("%s\n",buff);
       //and also if you want to save each byte
       bytes[i] = (unsigned char)atoi(buff);
       buff = strtok(NULL,".");
       i++;
    }
    free(buff);
    return 0;
}

Чуть-чуть переделал его под себя
Код
char sc[] = "150.156.16";
byte bytes[3] = "  ";
int i = 0;
char* buff;
buff = strtok(sc,(const char*)'.');
while (buff != NULL)
{
    bytes[i] = (byte)atoi(buff);
    buff = strtok(NULL,(const char*)'.');
    i++;
}

Но считывается только первое число (150), а дальше происходит выход из цикла, и я не пойму, почему. Подскажите, пожалуйста, какие условия из описания я не выполнил (P.S. ниже я привел описание функции, взятое из самого компилятора)?

Цитата
/** @name strtok
* ``A sequence of calls to the {\bf strtok} function breaks the
* string pointed to by {\bf s1} into a sequence of tokens, each of
* which is delimited by a character from the string pointed to by
* {\bf s2}. The first call in the sequence has {\bf s1} as its
* first argument, and is followed by calls with a null pointer
* as their first argument. The separator string pointed to by {\bf s2}
* may be different from call to call.
*
* ``The first call in the sequence searches the string pointed to
* by {\bf s1} for the first character that is {\it not} contained in
* the current separator string {\bf s2}. If no such character is found,
* then there are no tokens in the string pointed to by {\bf s1} and the
* {\bf strtok} function returns a null pointer. If such a character is
* found, it is the start of the first token.
*
* ``The {\bf strtok} function then searches from there for a character
* that {\it is} contained in the current separator string. If no such
* character is found, the current token extends to the end of the
* string pointed to by {\bf s1}, and subsequent searches for a token
* will return a null pointer. If such a character is found, it is
* overwritten by a null character, which terminates the current token.
* The {\bf strtok} function saves a pointer to the following character,
* from which the next search for a token will start.
*
* ``Each subsequent call, with a null pointer as the first argument,
* starts searching from the saved pointer and behaves as described
* above.
*
* ``The implementation shall behave as if no library function calls the
* {\bf strtok} function.
*
* ``This function is implemented in C, is not re-entrant and calls the
* functions: strspn, strpbrk, memchr.''
*
* @param s1 pointer to a string to begin searching, or null to continue
* searching a prior string
* @param s2 pointer to a string containing the set of separator characters
* @return ``The {\bf strtok} function returns a pointer to the first
* character of a token, or a null pointer if there is no token.''
*/
char *strtok (auto char *s1, auto const char *s2);
Go to the top of the page
 
+Quote Post



Reply to this topicStart new topic
1 чел. читают эту тему (гостей: 1, скрытых пользователей: 0)
Пользователей: 0

 


RSS Текстовая версия Сейчас: 20th July 2025 - 19:53
Рейтинг@Mail.ru


Страница сгенерированна за 0.01355 секунд с 7
ELECTRONIX ©2004-2016