Question

I am working on a university assignment and I've been wracking my head around a weird problem where my program calls strtok and never returns.

My code looks like:

int loadMenuDataIn(GJCType* menu, char *data)
{
   char *lineTokenPtr;
   int i;

   lineTokenPtr = strtok(data, "\n"); 
   while (lineTokenPtr != NULL) { 

   /* ... */

   }

}

I've looked up a bunch of sites on the web, but I cant see anything wrong with the way that I am using strtok and I cant determine why it would my code would get stuck on the line lineTokenPtr = strtok(data, "\n");

Can anyone help me shed some light on this?

(Using OSX and Xcode if it makes any difference)

Was it helpful?

Solution

have you checked the contents of the argument? is it \0 terminated?

the argument that you pass, is it writeable memory? strtok writes to the buffer that it gets as first argument when it tokenizes the string.

IOW if you write

char* mystring = "hello\n";

strtok(mystring,"\n"); // you get problems

OTHER TIPS

The function strtok() replaces the actual token delimiting symbols in the character string with null (i.e., \0) chars, and returns a pointer to the start of the token in the string. So after repeated calls to strtok() with a newline delimiting symbol, a string buffer that looked like

"The fox\nran over\nthe hill\n"

in memory will be literally modified in-place and turned into

"The fox\0ran over\0the hill\0"

with char pointers returned from strtok() that point to the strings the fox\0, ran over\0, and the hill\0. No new memory is allocated ... the original string memory is modified in-place, which means it's important not to pass a string literal that is of type const char*.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top