对于一个项目,我必须将组成的汇编语言翻译成内存位置和操作码。我需要解析文件两次,我使用 strtok() 来执行此操作。但是,我的程序没有到达第二个循环。(我在开头放了一个打印语句,所以我很确定它没有到达它。)我的想法是指针在文件的末尾,所以我试着把 rewind(filename) 和 fseek(filename, 0 , SEEK_SET),但似乎都没有解决问题。有什么想法吗?谢谢。
编辑:这是一些代码(第二遍代码)
j=1, i=0;
/*SECOND PASS*/
fseek(opcode,0,SEEK_SET);
char string[5];
while((fgets(str, buffer, file))!=NULL){/*getting line*/
printf("it got here. yup.");
fputs(memLoc(j-1), opcode); /*inserts the location in memory to file*/
fputs(" ", opcode);/*puts a space in the file*/
int state=0; /*state to see if on first or second part of code*/
for(i=0; i<buffer; i++){ /*gets rid of extra new line chars*/
if(str[i]=='\n'){
str[i]= '\0';
break;
}
}
cp = xerox(str);
token = strtok(cp, delimiters);
printf("%s ",token);
/*if it's not a label, find corresponding opcode and insert into file,
switch state, if it's a label, don't switch state*/
if((token!=NULL)&&token[strlen(token)-1]!=':'){
fputs(findOpCode(token), opcode);
state=1;
}
else state=0;
/*if it should be an opcode, insert corresponding opcode into file,
if it should be a location, find and insert where it should be*/
for (;token = strtok(NULL, delimiters);){
printf("%s ",token);
if(token!=NULL)
if(state==0){
fputs(findOpCode(token), opcode);
}
else if(state==1){
fputs(locate(token, fIndex), opcode);
}
}
j++;
fputc('\n', opcode);/*next line*/
}