NEED TO FIX: updated some code, but it still retrieves wrong message length

master
Aadhavan Srinivasan 2 years ago
parent 34861b4644
commit 9c87de7196

@ -44,20 +44,30 @@ char* fetch_message_string(char* message) {
} }
int end_index = end - message; int end_index = end - message;
if (*(message+ end_index-2) = '\r') { if (*(message + end_index-2) = '\r') {
num_of_terminators = 2; num_of_terminators = 2;
} else { } else {
num_of_terminators = 1; num_of_terminators = 1;
} }
end_index -= num_of_terminators;
int message_length = end_index - start_index; int message_length = end_index - start_index;
char* message_string = malloc(message_length + 1); char* message_string = malloc(message_length + 2);
printf("Message length is %d\n",message_length);
for (int i=0;i < message_length-num_of_terminators; i++) { /* The reason the upper-bound is message_length-1 is because the last for (int i=0; i<message_length; i++) { /* The reason the upper-bound is message_length-1 is because the last
character is a new-line, which the user would not have typed. */ character is a new-line, which the user would not have typed. */
*(message_string + i) = *(message + start_index + i); *(message_string + i) = *(message + start_index + i);
printf("%c",*(message_string + i));
} }
*(message_string + (message_length - num_of_terminators)) = '\0'; strcat(message_string,"\0");
// printf("%s\n",message_string);
// *(message_string + (message_length - num_of_terminators)) = '\0';
return message_string; return message_string;
} }

Loading…
Cancel
Save