出于某种原因,我的代码没有迭代检查"和"。为什么不拿起剩下的文字?
注意:我正在为另一个函数调用此函数。这个工作正常,只发布此参考:
int readFile(FILE *file, struct assem *item)
{
size_t i =0;
//this is sort of the constructor, if you will.
item->counter = 0;
//breaks down text file into line by line
size_t maxLines = sizeof item->myLines / sizeof *item->myLines;
//and stores them into "myLines array"
while(i < maxLines && fgets(item->myString, sizeof item->myString, file))
{
item->myLines[i] = strdup(item->myString);
item->myLines2[i] = strdup(item->myString);
i++;
item->counter++;
}
return 0;
}
int secondCheck(struct assem item)
{
for (int s = 0; s < item.counter; s++)
{
//Rformat check
if((strcmp(item.myWord[s],"add")==0)||(strcmp(item.myWord[s],"srl")==0)||
(strcmp(item.myWord[s],"and")==0)||(strcmp(item.myWord[s],"sub")==0)||
(strcmp(item.myWord[s],"slt")==0)||(strcmp(item.myWord[s],"nor")==0)||
(strcmp(item.myWord[s],"sll")==0)||(strcmp(item.myWord[s],"subu")==0)||
(strcmp(item.myWord[s],"sltu")==0)||(strcmp(item.myWord[s],"addu")==0)||
(strcmp(item.myWord[s],"or")==0))
{
checkRformat(item.myLines2[s], &item);
printf("%s%s%s%s%s%s\n",item.opcode,item.rs,item.rt,item.rd,item.shamt,item.funct);
}
else{printf("Womp");}
}
}
这是我的输出:
Splitting string:
add
$t1
$t1
$s6
000000NULLNULLNULL0000010100
这是我遇到问题的部分。
void checkRformat(char *LineArray, struct assem *item)
{
int i = 0;
char *str;
char *temp;
item->opcode = "NULL";
item->shamt = "NULL";
item->funct = "NULL";
item->rs = "NULL";
item->rt = "NULL";
item->rd = "NULL";
printf("Splitting string:\n");
temp = strdup(LineArray);
str = strtok(temp, " ,.-#\n\t");
//this helper function gets called by another.It primarily serves to convert the MIPS code
//instructions or labels into their binary values. These global variables will then concatenated
// and be printed onto a .txt file.
while(str != NULL)
{
printf("%s\n", str);
str = strtok(NULL, " ,.-#\n\t");
if (!str)
{
break;
}
if(strcmp(str, "add"))
{
item->opcode = "000000";
item->shamt = "00000";
item->funct = "10100";
}
else if(strcmp(str, "$t0"))
{
if(strcmp(item->rs, "NULL")){item->rs = "01000";}
else if(item->rt, "NULL"){item->rt = "01000";}
else if(item->rd,"NULL"){item->rd = "01000";}
}
else if(strcmp(str, "$t1"))
{
printf("\nSuccess\n");
if(strcmp(item->rs,"NULL")){item->rs = "01001";}
else if(item->rt,"NULL"){item->rt = "01001";}
else if(item->rd,"NULL"){item->rd = "01001";}
}
else if(strcmp(str, "$s6"))
{
if(strcmp(item->rs,"NULL")){item->rs = "11110";}
else if(item->rt,"NULL"){item->rt = "11110";}
else if(item->rd,"NULL"){item->rd = "11110";}
}
}
}
答案 0 :(得分:1)
strcmp
会返回 0 。
因此,所有条件都不会评估。