Experts please find the cause of infinite loop

This is a discussion on Experts please find the cause of infinite loop within the C Programming forums, part of the General Programming Boards category; i have an infinte loop that i cant find the cause.The terminating condition should be end of file . Code: ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    118

    Experts please find the cause of infinite loop

    i have an infinte loop that i cant find the cause.The terminating condition should be end of file .
    Code:
      FILE *f;
    f=fopen("office.out","r+"); 
    position=ftell(f);
      
       while(!feof(f))
        {
        fread(&p,sizeof(struct office),1,f);
        makeRecords(&p);
        fseek(f, position *( sizeof(struct office)),SEEK_SET);
        fwrite(&p, sizeof(struct office),1,f);
        
        position++;
        printf("am here"); }
        fclose(f);

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Look closely at your code ....
    Keep close track of what you are reading, where you are seeking to and what you are writing...
    You will never hit the end of the file because the file keeps getting bigger.
    Any EOF indication you may have gotten with your fread() call is reset by your fwrite() call.
    If you don't hit the end of the file on a READ operation, the loop never ends.

    Also you don't even check to see if the file actually opened or not.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by CommonTater View Post
    Look closely at your code ....
    Keep close track of what you are reading, where you are seeking to and what you are writing...
    You will never hit the end of the file because the file keeps getting bigger.
    Any EOF indication you may have gotten with your fread() call is reset by your fwrite() call.
    If you don't hit the end of the file on a READ operation, the loop never ends.

    Also you don't even check to see if the file actually opened or not.
    i am honestly looking through my code but cant find what am doing wrong.How does my file increase in size when am using fwrite which overwrites the content in the file

  5. #5
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Throw in a printf() for debugging and see what 'position' is equal to.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by sigur47 View Post
    i am honestly looking through my code but cant find what am doing wrong.How does my file increase in size when am using fwrite which overwrites the content in the file
    Ok so you do fread() and it hits the end of the file...
    Now you write to the file at a higher postion... position * sizeof(struct) ...
    the file gets bigger and the EOF flag is reset
    Now you come back to the top of your loop... what's it testing for?

    EOF is only signalled after a read operation fails against the end of the file.
    Writing to the file resets the flag.
    Seeking resets the flag (yes, you can seek past the end of the file).

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by CommonTater View Post
    Ok so you do fread() and it hits the end of the file...
    Now you write to the file at a higher postion... position * sizeof(struct) ...
    the file gets bigger and the EOF flag is reset
    Now you come back to the top of your loop... what's it testing for?

    EOF is only signalled after a read operation fails against the end of the file.
    Writing to the file resets the flag.
    Seeking resets the flag (yes, you can seek past the end of the file).
    Hi i have just tested my freads .Basically i have a file that contains 12 structures and after the 12th structure fread should return a 0 but mine returns a 1
    int finished=9;
    finished=fread(&p,sizeof(struct office),1,f);
    finished=fread(&p,sizeof(struct office),1,f);
    finished=fread(&p,sizeof(struct office),1,f);
    finished=fread(&p,sizeof(struct office),1,f);
    finished=fread(&p,sizeof(struct office),1,f);
    finished=fread(&p,sizeof(struct office),1,f);
    finished=fread(&p,sizeof(struct office),1,f);
    finished=fread(&p,sizeof(struct office),1,f);
    finished=fread(&p,sizeof(struct office),1,f);
    finished=fread(&p,sizeof(struct office),1,f);
    finished=fread(&p,sizeof(struct office),1,f);
    finished=fread(&p,sizeof(struct office),1,f);
    finished=fread(&p,sizeof(struct office),1,f);
    finished=fread(&p,sizeof(struct office),1,f);
    printf("%d\n",finished);

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    > Basically i have a file that contains 12 structures and after the 12th structure fread should return a 0 but mine returns a 1
    Yes, that is correct.

    You have to attempt to read record 13 before fread() will fail, and feof() will become true.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by Salem View Post
    > Basically i have a file that contains 12 structures and after the 12th structure fread should return a 0 but mine returns a 1
    Yes, that is correct.

    You have to attempt to read record 13 before fread() will fail, and feof() will become true.
    yes salem your right but if you look at the last code i posted ,my fread is on 13 and still no failure thats my question?

  10. #10
    Registered User
    Join Date
    Dec 2011
    Posts
    9
    hi
    I think you are reading from "office out" in r+ mode,and your writing to the same file.so it will be a infinite loop..because when write to the same file it will again not reaching EOF.
    i think so..if any mistake forgive me..

  11. #11
    Registered User
    Join Date
    Dec 2011
    Posts
    9
    fread returning 0 wen it fail to read ,so better you have to check it ,for come out of the loop .

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by sigur47 View Post
    yes salem your right but if you look at the last code i posted ,my fread is on 13 and still no failure thats my question?
    Read or RE-read messages 3 and 6 in this thread...

    Writing to the file resets the EOF flag.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 12-04-2011, 02:06 PM
  2. Replies: 3
    Last Post: 10-14-2011, 11:33 PM
  3. Infinite loop
    By slippy in forum C Programming
    Replies: 8
    Last Post: 05-28-2006, 12:39 AM
  4. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 11:17 AM
  5. Infinite #define loop i can't find a way around
    By IonBlade in forum C++ Programming
    Replies: 6
    Last Post: 12-18-2003, 08:38 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21