OK so I'm trying to parse simple URL's from a whole char chunk using strtok but always keeps giving me an unhandled exception error . SO here is all the code in C++ :

char* str = "(%/\%)google.com(%/\%)hotmail.com(%/\%)nananna.org(%/\%)"
	char *url[];
	while(str != NULL){
		url[0] = strtok(str, "(%/\%)");
		    if (DownloadFile_1(url[0],szPath) == 0);{// Writes "URL 1"
			ExitProcess(0); 
		   }
		url[1] = strtok (NULL, "(%/\%)");
		   if (DownloadFile_1(url[1],szPath) == 0);{// Writes "URL 2"
			ExitProcess(0); 
		   }
		url[2] = strtok (NULL, "(%/\%)");
		   if (DownloadFile_1(url[2],szPath) == 0);{// Writes "URL 3"
			ExitProcess(0); 
		   }
	}

EDIT** : str can only be a const or char* because the function that I'm using with it uses const char.

Recommended Answers

All 4 Replies

strtok modifies the source string. You have no choice but to make a copy if the original string can't be modified. It also looks like you need to study about arrays a bit.

strtok modifies the source string. You have no choice but to make a copy if the original string can't be modified. It also looks like you need to study about arrays a bit.

OK, and yeah I shortly began C++ and sincerely I found nothing that says it modified the variable str {original}, all I found was that it was unsafe :/

That doesn't look very C++ish to me, specifically because you're using a C function.

You can more than likely use a C++ std:;string, couldn't you? The c_str() member returns a const char *.

http://www.cplusplus.com/reference/algorithm/
http://www.cplusplus.com/reference/string/string/

A C++ stringstream may fulfill your tokenization needs, http://www.cplusplus.com/reference/iostream/stringstream/

all I found was that it was unsafe :/

"Unsafe" is often a code word for "I don't understand how to use it correctly". Off the top of my head, I can only think of one function in the standard library that's legitimately unsafe, and it's not strtok. ;)

That doesn't look very C++ish to me, specifically because you're using a C function.

And the C++ version of strtok is...wait for it...strtok. Or manually parsing the string yourself. Or using a third party library that does it for you.

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.