So I'm trying out some parsing right now, it's just some simple if statement checking, and I can't get it to read in the last character from a file properly. I've been screwing around in the code for several hours and have only learned that the problem is in the getnext_arg function.
Thanks for any help you can give.
Code:#include "stdio.h" #include "string.h" #include "stdlib.h" char gotten_word[35]; int getnext_arg(FILE * pfile){ int gotten_char='a'; int i = 0; if(pfile != NULL){ while (gotten_char != EOF && gotten_char != '\n' && gotten_char != ' '){ gotten_char = getc(pfile); printf("GC: %c\n", gotten_char); if(gotten_char == EOF){ return 2; }else if(gotten_char != '\n' && gotten_char != ' '){ gotten_word[i] = gotten_char; i++; } } }else{ printf("Error: Null file\n"); return 1; } return 0; } int ifequ(int c1, int c2){ if(c1 == c2) { return 1; }else{ return 0; } } int ifnequ(int c1, int c2){ if(c1 != c2) { return 1; }else{ return 0; } } int exeCode(){ printf("Executing\n"); } int nexeCode(){ printf("Not executing\n"); } int main(void){ FILE * pfile; int comp1 = ' '; int comp2 = ' '; char compOp[2]; char path[45]; int ret; printf("Enter path to file: "); gets(path); pfile = fopen(path, "r"); if(pfile != NULL){ while(getnext_arg(pfile) != 2){ if( !strcmp(gotten_word, "if") ){ getnext_arg(pfile); comp1 = atoi(gotten_word); printf("%d\n", comp1); getnext_arg(pfile); if(!strcmp(gotten_word, "==") || !strcmp(gotten_word, "!=")){ strcpy(compOp, gotten_word); printf("%s\n", compOp); }else{ printf("Error: Invalid operator."); return 1; } getnext_arg(pfile); printf("%s\n", gotten_word); comp2 = atoi(gotten_word); printf("%i\n", comp2); if( !strcmp(compOp, "==")){ ret = ifequ(comp1, comp2); } if( !strcmp(compOp, "!=")){ ret = ifnequ(comp1, comp2); } if(ret == 1){ exeCode(); }else{ nexeCode(); } } } } else { printf("Null error\n"); } return 0; }



LinkBack URL
About LinkBacks


