I have some code that is meant to read the first 3 characters from a character array that is read from a file, it was working then without changing anything is stopped working. The 'command' char array use to hold "and" but now often holds "add▒" and sometimes "and0" but I've only declared it to be 3 long yet it still manages to hold more. Is there some context I am missing?
//ORIGINALLY THIS WORKED
for (i = 0; i < 3; i++){
command[i] = line[i];
}
/*Interpret AND or ADD or JMP */
if (strcmp(command,"and") == 0){
hexLine[0] = changeHex(5);
}else if (strcmp(command,"add") == 0){
hexLine[0] = changeHex(1);
}else if (strcmp(command,"jmp") == 0){
hexLine[0] = changeHex(12);
}
printf("%s", command);
//AND NOW THIS DOESNT WORK
for (i = 0; i < 3; i++){
command[i] = line[i];
}
/*Interpret AND or ADD or JMP */
if (strcmp(command,"and") == 0){
hexLine[0] = changeHex(5);
}else if (strcmp(command,"add") == 0){
hexLine[0] = changeHex(1);
}else if (strcmp(command,"jmp") == 0){
hexLine[0] = changeHex(12);
}else if (strcmp(command,"ld ") == 0){
hexLine[0] = changeHex(2);
}
printf("%s", command);
command?forloop copies 3 characters but doesn't zero-terminatecommand. Sostrcmpwon't behave the way you want. Putcommand[i] = 0;after yourforloop.