0

I am getting an infinite loop with my program. My instructor told me to only use while(!feof(filename)) but I feel thats the reason I'm having this problem.

What the program is supposed to do is take 3 strings and an int from a datafile and the 2nd and 3rd with the command line arguments via strcmp. My program instead just goes straight to the else statement and thats where my looping problem happens.

heres a snippet of the datafile im reading from. note the infinite loop only prints "Tide!":

21600 2055551111 2055552222 1 Roll
21601 2055552222 2055551111 1 Tide! 

and heres the code:

void skipText(int size, FILE* textfile) {

    int i=0;
    char line[50];

    for (i=0; i<size; i++) {
        fscanf(textfile, "%s", line);
        printf("%s ", line);
    }
}


int main(int argc, char* argv[]) {

    char posix[30];
    char phone1[30];
    char phone2[30];
    int textsize=0;

    printf("\n\nTime                           %s                           %s", argv[2], argv[3]);
    printf("\n======================================================================================\n\n\n");

    FILE* textfile= fopen(argv[1],"r");

    if (textfile==NULL) {
        printf("File Doesn't Exist\n");
        return -1;
    }   

    fscanf(textfile, "%s %s %s %d", posix, phone1, phone2, &textsize); 

    while ( !feof(textfile) ) { 

        if ( (strcmp(argv[2],phone1)==0) && (strcmp(argv[3],phone2)==0) ) { //1st to 2nd
            printf("I WORK!");
            //printText(posix, phone1, phone2, textsize, line, textfile);
            //readableTime(posix);
             }


        else  skipText(textsize, textfile);

    fscanf(textfile, "%s %s %s %d", posix, phone1, phone2, &textsize); 

}


    fclose(textfile);
    return 0;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
Corri
  • 1
  • 1
  • Yes, [`while(!feof(filename))` is considered as bad](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) in normal case. -- But you are using `feof()` after trying to read and before using data read, so it seems okay. – MikeCAT Mar 05 '17 at 05:02
  • Yea I think the problem has something to do with the "if strcmp" statement not ever being executed but I don't know why. – Corri Mar 05 '17 at 05:17

0 Answers0