Skip to main content
gamedev.net
🔒 Locked

strcat problems

Started by Codorke Aug 2, 2005 at 12:23 PM 2 replies 400+ views
Original Post
Codorke
Codorke
Could someone tell me whats wrong ? int num_pages = get_total_text_pages(); char buffer[5]; for(int i=0; i<MAX_PAGES; i++) { // Marked Pages if(i<=num_pages) { string_before = "<page nr='"; _itoa(i+1, buffer, 10); strcat(string_before, buffer); strcat(string_before, "' marked='"); if(StrToInt(find_in_text(S, string_before, '\'')) == 1) set_marked_pages(i, true); else set_marked_pages(i, false); }else set_marked_pages(i+1, false); } Thanks in advance
izhbq412
izhbq412
The problem is
string_before = "<page nr='";
string_before is pointing to a constant string. You'd want a strcpy here.
___The essence of balance is detachment. To embrace a cause, to grow fond or spiteful, is to lose one''s balance after which, no action can be trusted. Our burden is not for the dependent of spirit. - Mayar, Third Keeper
Enigma
Enigma
To expand on that slightly, string_before is a pointer to a character. The expression string_before = "
int num_pages = get_total_text_pages();int required_size;char * string_before;for (int i = 0; i < MAX_PAGES; ++i){	if (i < num_pages)	{		required_size = snprintf(0, 0, "<page nr='%i' marked='", i + 1);		if (required_size > 0)		{			string_before = (char *)malloc(required_size + 1);			if (!string_before || snprintf(string_before, required_size + 1, "<page nr=%i marked='", i + 1) != required_size)			{				// error			}		}		else		{			// error		}		// the rest of your code		free(string_before);	}}


C++:
int num_pages = get_total_text_pages();for (int i = 0; i < MAX_PAGES; ++i){	std::string string_before = "<page nr='" + boost::lexical_cast< std::string >(i + 1) + "' marked='";	// rest of your code, possibly using string_before.c_str() if interfacing with C code.}

Enigma
Codorke
Codorke
Thanks fort helping me out !
This works indeed.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.