如果我正在检查fscanf
或'+'
或'-'
等,我会在'%'
中测试什么。
例如,如果我正在测试int,我会这样做:
if(fscanf(read,"%d", &d)) //do something else if (fscanf(read,"%s", s)) // do something else else if fscanf(read,"%?", ?) //case for + - % /
相关代码:
while(feof(read) == 0){
//printf("help me");
if(fscanf(read,"%d", &d)) {
/* this makes sure that ints preceding "pick" etc are not
* counted as ints that are pushed onto the stack. Further this makes sure
* the testing for this does not "eat" and of the words in the file
*/
//if (d == '+') {
//fprintf (write, "\t+ DETECTED\n");
//}
const long pos = ftell(read); // stores current position
//checks if the next thing
if (fscanf(read,"%s", s)){
if ((strcmp(s , "pick") == 0 || (strcmp(s , "eq") == 0))){
//fprintf (write, "%d %s called\n", d, s);
}
else {
push(d, write);
//undo the word we "ate"
fseek(read, pos, SEEK_SET);
}
} else push(d, write);
}
else if (fscanf(read,"%s", s)){
if (strcmp(s , "defun") == 0) {
fscanf(read,"%s", s);
printdefun (s, write);
}
else if (strcmp(s , "return") == 0) {
printreturn (write);
}
else if (strcmp(s , "return") == 0) {
fprintf (write, "\t+ detected\n");
}
}
else if (fscanf(read,"%c", &c)){
if (c == '+') {
fprintf (write, "\t+ DETECTED\n");
}
}
文本文件:
defun main
15 10 +
return
请注意,“+ detected”永远不会被打印