Two quick questions:
Using g++ (gcc-2.95.1) to compile, I've been having no luck with strtok. So I dig up a simple strtok example from the internet to see what's up (see below). This complies with out error, but when I run the executable (under FreeBSD) I get the message: Bus error (core dumped). What might the problem be?
#include <string.h>
#include <stdio.h>
#include <iostream.h>
int main()
{
char* s1 = "tokenize.me. i am a.tokenizer string";
char* s2 = ". "; // delimiters are '.' and ' ' (space)
char* next_token = strtok(s1, s2);
while (next_token) // strtok returns 0 if no tokens left
{
// print each token to the screen
cout << next_token << endl;
next_token = strtok(0, s2); // must pass null pointer to
// continue to tokenize s1,
// or else tokenization restarts
}
return 1;
}
Also, I'd like to be able to "strcat" an integer to a character string (using a variable such as "i-1", not a fixed "5" e.g.) but the "itoa" function is unsupported in g++. Is there an easy work around to convert an integer into a character?
Using g++ (gcc-2.95.1) to compile, I've been having no luck with strtok. So I dig up a simple strtok example from the internet to see what's up (see below). This complies with out error, but when I run the executable (under FreeBSD) I get the message: Bus error (core dumped). What might the problem be?
#include <string.h>
#include <stdio.h>
#include <iostream.h>
int main()
{
char* s1 = "tokenize.me. i am a.tokenizer string";
char* s2 = ". "; // delimiters are '.' and ' ' (space)
char* next_token = strtok(s1, s2);
while (next_token) // strtok returns 0 if no tokens left
{
// print each token to the screen
cout << next_token << endl;
next_token = strtok(0, s2); // must pass null pointer to
// continue to tokenize s1,
// or else tokenization restarts
}
return 1;
}
Also, I'd like to be able to "strcat" an integer to a character string (using a variable such as "i-1", not a fixed "5" e.g.) but the "itoa" function is unsupported in g++. Is there an easy work around to convert an integer into a character?