1

Here i have one string means character array. this array contains separator character also. Now i want to take out only strings without this separator.

I am using strtok function.

I want to separate all my words with this separator.

But here i am getting some problems in this code.

Please Let me know if i am doing something wrong or tell me different way simple and easy way to achieve this thing,consider memory risk also.

Here this line pch = strtok (str,&sep); get nothing. Problem is here only.

Code

/* strtok example */
#include <stdio.h>
#include <string.h>

#define SEPRATOR 0x03

int main ()
{
    char str[1024];
    int count = 0;
    char sep;
    memset(str,0,1024);
    sep = SEPRATOR;

    memcpy(str,"1",strlen("1"));
    count = count + strlen("1");
    str[count++] = sep;
    memcpy(str+count,"0",strlen("0"));
    count = count + strlen("0");
    str[count++] = sep;
    memcpy(str+count,"2",strlen("2"));
    count = count + strlen("2");
    str[count++] = sep;
    memcpy(str+count,"abc_1.0.xyz",strlen("abc_1.0.xyz"));
    count = count + strlen("abc_1.0.xyz");
    str[count++] = sep;
    memcpy(str+count,"3",strlen("3"));
    count = count + strlen("3");
    str[count++] = sep;
    memcpy(str+count,"23455.456",strlen("23455.456"));
    count = count + strlen("23455.456");

    printf("Input String = %s\n",str);

    char * pch;
    int pchcount = 0;
    pch = strtok (str,&sep);
    while (pch != NULL)
    {
        pchcount++;
        printf ("%d === %s\n",pchcount,pch);
        pch = strtok (NULL,&sep);
    }
    return 0;
}
mmmmmm
  • 32,227
  • 27
  • 88
  • 117
sam_k
  • 5,983
  • 14
  • 76
  • 110
  • @chris The question you suggested is for [tag:C++]. This question is tagged as [tag:C]. However, this question is a possible duplicate of [Tokenizing strings in c](http://stackoverflow.com/questions/266357/tokenizing-strings-in-c). – Eitan T Jul 10 '12 at 17:28
  • @EitanT, My bad, it was tagged C++ when I went looking for the FAQ one. – chris Jul 10 '12 at 17:48

1 Answers1

6

The second argument to strtok() is a null terminated string (char*), but you passing the address of a single char. While a char*, it will not be null terminated. Change to:

char sep[2] = { SEPRATOR, 0 };

Changing current uses of sep accordingly:

str[count++] = sep[0];

pch = strtok(str, sep);

You may find snprintf() simpler to construct your str than the sequence of memcpy() invocations.

Also, if you want to initialise str:

char str[1024] = ""; /* instead of performing a memset(). */
hmjd
  • 120,187
  • 20
  • 207
  • 252
  • So how can i do this? What is the solution – sam_k Jul 10 '12 at 17:12
  • thanks for Your response. Its necessary to free pch pointer which i used in my code? Because i have to take care of memory issue also. – sam_k Jul 10 '12 at 17:17
  • @hmjd Can you please explain me Why we dnt require to free pch pointer. Because i have also confusion about this. Thanks in Advance – user1089679 Jul 11 '12 at 07:23
  • @user1089679, `strtok()` iterates over the buffer passed to it. The return value from `strtok()` is referring to different locations within that buffer, it performs no `malloc()`. – hmjd Jul 11 '12 at 07:28