Char split

closed account (G26pX9L8)
Hey all,

I have this textline in a char:

0x0BOF00x0S0x00x0EOF00x0S0x07101
or in pieces:
0x0BOF0 0x0S0x0 0x0EOF0 0x0S0x0 7101

but when I split it (split on 0x0S0x0) using the strtok function I get this:

BOF
EOF
711

Not only the 0x0S0x0 is gone but also the other 0 and x but that should be the same. What i expected is this:

0x0BOF0
0x0EOF0
7101

The code I am using:
1
2
3
4
5
6
7
8
char *token = strtok("0x0BOF00x0S0x00x0EOF00x0S0x07101
", "0x0S0x0");

while( token != NULL )
{
  cout<<token<<endl;
  token = strtok(NULL, SEPA_MSG);
}

I hope the problem is clear and somebody have an answer.
Last edited on
you shouldn't modify a string constant.
man page says...

1
2
3
4
5
6
7
8
9
10
11
12
BUGS
       Avoid using these functions.  If you do use them, note that:

              These functions modify their first argument.

              These functions cannot be used on constant strings.

              The identity of the delimiting character is lost.

              The strtok() function uses a static  buffer  while  parsing,  so
              it’s not thread safe.  Use strtok_r() if this matters to you.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <boost/regex.hpp>
using namespace std;


int main(int argc, char ** argv) {

    const char * token = "0x0BOF00x0S0x00x0EOF00x0S0x07101";
    string s(token);

    boost::regex reg("0x0S0x0");
    boost::sregex_token_iterator it(s.begin(), s.end(), reg, -1);
    boost::sregex_token_iterator end;

    for ( ;it != end; ++it ) {
        cout << *it << endl;
    }
    return 0;
}



1
2
3
4
$ ./1
0x0BOF0
0x0EOF0
7101

strtok doesn't work that way
http://www.cplusplus.com/reference/clibrary/cstring/strtok/
The second argument string's chars are the delimiters -> if strtok finds any single of those in the string which it searches, it will return.

You want to search for an entire string, not just single chars.

If you want to stick to the C lib then use strstr()
http://www.cplusplus.com/reference/clibrary/cstring/strstr/

otherwise look into std::string functions, much easier to use.
http://www.cplusplus.com/reference/string/string/
http://www.cplusplus.com/reference/string/string/find/
Last edited on
closed account (G26pX9L8)
@bigearsbilly
Where can I found the regex.hpp?

@R0mai
Its not about finding a string but splitting using a loop. If I wish to find a string I should use the strstr function but thats not the issue here.
Last edited on
closed account (G26pX9L8)
Do I realy need a special library? I want to keep it as small as possible. Is there no other solution?
Of course you don't need a special library. All you need to do is search for something that tells your tokens apart from each other (be it a string or a single character), and you know where to make your split. That's why R0mai gave you those functions.

-Albatross
Its not about finding a string but splitting using a loop.

You can use those functions in a loop too:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <cstring>
#include <string>

void c_split() {
    const char *str = "0x0BOF00x0S0x00x0EOF00x0S0x07101"; 
    const char *delim = "0x0S0x0";
    
    const char *start = str;
    const char *end;
    
    while( end = strstr(start, delim) ) {
        printf("\"%.*s\"\n", end-start, start);
        start = end + strlen(delim);
    }
    printf("\"%s\"\n", start);
}

void cpp_split() {
    std::string str = "0x0BOF00x0S0x00x0EOF00x0S0x07101";
    std::string delim = "0x0S0x0";

    unsigned start = 0;
    unsigned end;

    while( (end = str.find(delim, start)) != std::string::npos ) {
        std::cout << '"' << str.substr(start, end-start) << '"' << std::endl;
        start = end + delim.length();
    }
    std::cout << '"' << str.substr(start) << '"' << std::endl;
}

int main() {
    std::cout << "C style split:\n";
    c_split();    
    std::cout << "\nC++ style split:\n";
    cpp_split();

    return 0;
}
closed account (G26pX9L8)
@R0mai
tnx it works, I will search it out further tomorrow
Last edited on
Topic archived. No new replies allowed.