Thread: loop-exit problem

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    37

    loop-exit problem

    I have a problem with a loop refusing to stop at the end of file.
    Here's the code.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <string.h>
    
    #define CHRS 41
    #define RANKSIZ 10
    
    void main(void)
    {
       FILE *rslpf;                       /* Pointer to file description */
       long B_dd, B_mm, B_yyyy, En_dd, En_mm, En_yyyy, result;   
       char rank[RANKSIZ], stations[CHRS], gender, FulName[30] ;
    
       if ((rslpf = fopen("C:\\rslpf.txt", "r")) == NULL)
       {  printf("Cannot open file\n");
          exit(EXIT_FAILURE);
       }
       while (fscanf(rslpf, "%i%*c %[^/]%*c  %[^/]%*c  %c%*c %i%*c  %i%*c  %i%*c %i%*c  %i%*c  %i%*c  %[^\n]%*c ",
              &result, rank, FulName, &gender, &B_dd, &B_mm, &B_yyyy, &En_dd, &En_mm, &En_yyyy, stations) != EOF)
       {
                printf("Officer: \nGender: \nDate of Birth: \nDate of Enlistment: \nDepartment: ");
                gotoxy(22,1);
                printf("%s %i %s", rank, result, FulName);
                gotoxy(22,2);
                printf("%c", gender);
                gotoxy(22,3);
                printf("%i/%i/%i", B_dd, B_mm, B_yyyy);
                gotoxy(22,4);
                printf("%i/%i/%i", En_dd, En_mm, En_yyyy);
                gotoxy(22,5);
                printf("%s", stations);
                getchar();
                clrscr();
        }
       fclose(rslpf);   
    }
    I'd appreciate your help...

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    while (fscanf(rslpf, "%i%*c %[^/]%*c  %[^/]%*c  %c%*c %i%*c  %i%*c  %i%*c %i%*c  %i%*c  %i%*c  %[^\n]%*c ",
              &result, rank, FulName, &gender, &B_dd, &B_mm, &B_yyyy, &En_dd, &En_mm, &En_yyyy, stations) != EOF)
    That is pretty horrid. You'll probably find that you're not reading in correctly, and the program is stuck in a loop well before EOF. You'd do better by using fgets(), then sscanf(), at least that way you spot problems easier.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    37
    I suspect my end-of-file indicatior may not be set. Everything in the file is read and displayed. The loop just doesn't end.
    The data was entered into the file using multiple functions bcos I had to validate each one. And a '/' was placed between them at the time they were written. Is there any way that I could set the end of file indicator before hand (immediately after the final field has been input)?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    EOF doesn't get "not set". To prove it, do this:
    Code:
    int foo;
    if ((rslpf = fopen("C:\\rslpf.txt", "r")) == NULL)
       {  printf("Cannot open file\n");
          exit(EXIT_FAILURE);
       }
       while ( (foo = fgetc( rslpf )) != EOF )
            printf("%c %d\n", foo, foo );
    This will prove that Hammer's correct, and you're not.

    Oh, and main is not a void function. It returns an int. Always.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    37
    Absolutely right quzah. Thanks alot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with a loop
    By wiggsfly in forum C Programming
    Replies: 5
    Last Post: 11-30-2008, 12:45 PM
  2. why it can not exit the while loop
    By letmoon in forum C Programming
    Replies: 3
    Last Post: 10-30-2008, 09:59 AM
  3. whats the problem....
    By vapanchamukhi in forum C Programming
    Replies: 3
    Last Post: 09-05-2008, 12:19 PM
  4. having problem with string statement during a loop!
    By Hp7130p in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2005, 09:40 AM
  5. While loop problem
    By nadkingcole in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 06:14 AM