I've made this rather messy method that is supposed to split the message parameter (max 160 byte) into 60-byte strings if it is larger than 60 bytes. The message is always between quotes so I also want to ignore them and just get whats between the quotes.

Example: "Hi there everybody" //I want Hi there everybody

The problem with this method is when the printf at the end is executed it doesn't output what it's supposed to.
With the message "Hello everybody" the output is:
tempString =
tempString2 =
tempString 3 = u

I bet it's just a silly stupid mistake, but I just can't figure out whats wrong.

void saveMessage(char *message) {
	int i = 1; //1 to skip the first "
	int isToManyCharacters = 0;
	int currentTempString = 1;
	char tempString1[60];
	char tempString2[60];
	char tempString3[60];
	
	while (message[i] != '"') {
                        //If under 60 byte fill up tempString1
		if(i<60) {
			tempString1[i] = message[i];
			printf("command:61 tempString1[i] = %c     message[i] = %c\n", tempString1[i], message[i]);
			if (i+1 == 60) {
				tempString1[i+1] = '\0';
				currentTempString++;
			}
		}
                        //If over 60 bytes but under 120 fill up tempString2
		else if(i==60 || i<120) {
			tempString2[i-60] = message[i];
			if (i+1 == 120) {
				tempString2[i+1] = '\0';
				currentTempString++;
			}
		}
                       //If over 120 bytes but under 160 fill up tempString3
		else if(i==120 || i<160) {
			tempString2[i-60] = message[i];
			if (i+1 == 160) {
				tempString2[i+1] = '\0';
				isToManyCharacters = 1;
			}
		}
		
		i++;
	}

//Adding the 0-byte
	if (currentTempString == 1)
		tempString1[i] = '\0';
	else if (currentTempString == 2)
		tempString2[i] = '\0';
	else if (currentTempString == 3)
		tempString3[i] = '\0';
	
	printf("tempString = %s\ntempString2 = %s\ntempString 3 = %s\n",tempString1, tempString2, tempString3);
}

Recommended Answers

All 4 Replies

You've done some mistake at line 14, 22, and 30.

Actually, when you declare
char TempString[60];
it means you've allocated 60 char with index from 0 to 59. Hence you can't access TempString[60].

May my advice useful.

Thanks alot. I changed my code, but unfortunatly it didn't fix the problem, and when I woke up this morning I got a segmentation-fault.

GDB- only gives me this info:
55933 [main] ifisms 5736 _cygtls::handle_exceptions: Exception:
STATUS_ACCESS_VIOLATION
57244 [main] ifisms 5736 _cygtls::handle_exceptions: Error while dumping state (probably corrupted stack)

I belive its happening in the last line of the code cause if I put in a test printf after the last line it's newer executed.

//Saves the new message in the number of datablocks specified by the parameter 
void saveMessage(char *message, meta_data *metData) {
	int i = 1; //1 to skip the first "
	printf("command:48 message = %s\n", message);
	int isToManyCharacters = 0;
	int messageLength = strlen(message);	
	int currentTempString = 1;
	char tempString1[60];
	char tempString2[60];
	char tempString3[60];

	while (message[i] != '"') {
		if(i<61) {
			if (i+1 == 61) {
				tempString2[i] = '\0';
				currentTempString++;
			}
			else
				tempString1[i] = message[i]; 			
		}
		else if(i==60 || i<120) {
			if (i+1 == 120) {
				tempString2[i] = '\0';
				currentTempString++;
			}
			else
				tempString2[i-60] = message[i];
		}
		else if(i==120 || i<160) {
			if (i+1 == 160) {
				tempString2[i+1] = '\0';
				isToManyCharacters = 1;
			}
			else
				tempString2[i-120] = message[i];
		}
		i++;
	}
	if (currentTempString == 1 && i<60)
		tempString1[i] = '\0';
	else if (currentTempString == 2 && i<120)
		tempString2[i] = '\0';
	else if (currentTempString == 3)
		tempString3[i] = '\0';

	printf("command:93\ntempString = %s\ntempString2 = %s\ntempString 3 = %s\n",tempString1, tempString2, tempString3);			
}

Maybe you should change this part

char tempString1[60];
char tempString2[60];
char tempString3[60];

into

char tempString1[61];
char tempString2[61];
char tempString3[61];

Thank you. I got it to work now. The error wasn't in this method after all.but another place I tried to assign a value to something without mallocing.

It works perfect now.

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.