Splitting a string

Hi Guys,

I’m trying to split a string into substrings by using the strtok function, my Q2686H keeps on restarting when this part of the code is reached. At this point in time I assume it has something to do with memory, please help

The incoming string will look something like this:

admin1:John,Doe,+27821231234

I want:

str[1] = “John”
str[2] = “Doe”
str[3] = “+27821231234”

so that I can write it to flash with ald_flhWrite.

Here is my code:

char string[40],term[3],*ptr;
char str1[3][30];
int cnt=0;
	
strcpy(string,inputString);
strcpy(term,",:");

ptr = strtok(string,term);
	
while(ptr!=0)
{
	strcpy(str1[cnt],ptr);
	ptr = strtok(0,term);
	cnt++;
}
strcpy(name,str1[1]);
strcpy(surname,str1[2]);
strcpy(mobile,str1[3]);

Regards

Most likely, it’s a standard ‘C’ issue to do with bad pointers and/or running off the end of the string - not specifically Wavecom-related.

Note that strtok is part of the ANSI standard library - so have you checked that your basic algorithm is correct by running it on another ‘C’ platform; eg, MSVC, Borland, MINGW, etc…?

Hi awneil

I have, as a matter of fact, tried running it on a PIC micro and it works fine, no problems. Any suggestions? Should I not maybe use adl_memGet to allocate memory to my string named str? str is however, a 2d array! I’m a little confused right now :slight_smile: Has no one on this forum tried splitting a string with strtok, or am I missing something?

Regards

NolanWade

And you’re sure it’s identical?

If the problem is due to bad pointers and/or string (mis-)termination, it’s possible that you might just be “lucky” on the PIC…

Note that some Wavecom API’s don’t give you a string - they just give you a buffer and a length (no NUL terminator)…

How are you sure that the problem lies in splitting the strings?

Have you tried single-stepping it to see exactly where it fails?

It has worked fine for me!

Any example code to go by? I am aware of the NULL terminator :slight_smile:

in c an index of an array starts at 0, not at 1. You probably trigger a watchdog exception.

he doesn’t want ‘admin’ which would be [0], so there’s nothing wrong with starting at [1] in this case.

Correct, but not relevant; the problem is ending at 3, strcpy(mobile,str1[3]);

char string[40],term[3],*ptr;
char str1[4][30];
int cnt=0;
   
strcpy(string,inputString);
strcpy(term,",:");

ptr = strtok(string,term);
   
while(ptr!=0)
{
   strcpy(str1[cnt],ptr);
   ptr = strtok(0,term);
   cnt++;
}
strcpy(name,str1[1]);
strcpy(surname,str1[2]);
strcpy(mobile,str1[3]);

Hi Jeroen, I conclude with your comment. str1 should be char str1[4][30], just a typo from my side. My mistake or error lies somewhere else in my code then, off to find that bug! Will update you on my progress! :smiley:

char term[3];
char *ptr;
char str1[4][30];
int cnt = 0;

Hi guys, there seems to be an error with the above variable declarations. It is somewhere in those four lines, where the Q2686H hangs and the WDT resets the hardware. Where can I find some good GCC compiler & ADL documentation as I find the quality of the Wavecom docs terrible.

Regards

NolanWade

Really? How did you determine that?

It seems very unlikely that just variable definitions would cause a fault - unless they are local (automatic) variables, and you are overflowing the stack?

For the GCC compiler, it’s just the standard GNU documentation.
AFAIK, there is nothing special about the GCC used by Wavecom - so any general materials from anywhere about the same GCC version will be applicable.

ADL is entirely Wavecom-proprietary - so the Wavecom documentation is all there is, I’m afraid.

Unfotunately, this is true.
Open-AT is a great thing; it has only 3 problems:

  1. Documentation;
  2. Documentation;
  3. Documentation.

Having said that, I did find that GNU’s GCC documentation - particularly the library documentation - was not at all easy to find!

Try:
gcc.gnu.org/onlinedocs/
gnu.org/software/libc/libc.html

char name[20];
char surname[20];
char mobile[20];

ascii * string = adl_memGet(sizeof(name)+sizeof(surname)+sizeof(mobile));
const char term[] = ":,";
char *tok;

strcpy(string,SmsText);

tok = strtok (string, term);
tok = strtok (NULL, term);
strcpy(name,tok);
tok = strtok (NULL, term);
strcpy(surname,tok);
tok = strtok (NULL, term);
strcpy(mobile,tok);

Thanks for your input awneil :slight_smile: The above code works fine, not too elegant, but it works!!