So I'm trying to remove 2 brackets from lines I'm reading from a file.
For some reason when I run my program the characters in between the delimeters is removed.

for example
greetings pe()ple
would end up as
greetings pe

I'm using a char array of delimiters.
char delimiters[] = " ( )";

Why does strtok delete the characters between the parenthesis?

Remember that strtok (which frankly I would avoid using if possible) works by overwriting the delimiters with '\0', it is not deleting anything. It returns pointers into the original string.

So when you call

char *token;
token = strtok("greetings pe()ple", "()");

it scans forward until it finds the '(', replaces it with '\0' and returns a pointer to the 'g' giving a string of "greetings pe". If you then call strtok again it

token = strtok(NULL, "()");

It scans past the ')' to the end of the string, finding no more delimiters it returns a pointer to "ple". At the end of this your original string now contains

"greetings pe\0)ple"

Nothing has been deleted but the '(' has been overwritten.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.