#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    107
    Rep Power
    12

    wrong type of loop ?


    Hi,

    both of these loops are writing an extra record in a text file, but I do not understand why ?

    Code:
    void read_record(FILE ** fp)
    {
    char current_record[BUFFER_SIZE];
    
       do
       {
          fgets(current_record, BUFFER_SIZE, fp[IN_FILE]);
    
          fputs(current_record, fp[FIN_FILE]);
       }  while(!feof(fp[IN_FILE]));
    }
    
       while(!feof(fp[IN_FILE]))
       {
          fgets(current_record, BUFFER_SIZE, fp[IN_FILE]);
    
          fputs(current_record, fp[FIN_FILE]);
       }

    tia,
  2. #2
  3. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    are you sure the extra line really isn't in the original file?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    107
    Rep Power
    12
    yes

    IN_FILE refers to a text file with 10 different records and FIN_FILE refers to a newly created file and has 11 records, the 10th and 11th are identical

    :(
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    272
    Rep Power
    19
    feof doesn't return a 0 until after a failed read. In both of your loops you read the last record and write it out. You then check for feof but it hasn't been triggered yet. You then try to read again and it fails. Since nothing was read in the buffer still contains the previous read's data and you write that out. Then the check for feof gets you out of the loop.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    107
    Rep Power
    12
    oooops


    okay. I'll try something else

    thanks 3dfxMM
  10. #6
  11. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,114
    Rep Power
    1803
    Originally posted by cplusplos
    oooops


    okay. I'll try something else

    thanks 3dfxMM
    Something else like:

    Code:
    do
    {
        int check = fgets(current_record, BUFFER_SIZE, fp[IN_FILE]);
        if( check != EOF )
        {
             fputs(current_record, fp[FIN_FILE]);
        }
    }  while( check != EOF);
    or

    Code:
    while( fgets(current_record, BUFFER_SIZE, fp[IN_FILE]) != EOF )
    {
        fputs(current_record, fp[FIN_FILE]);
    }
    The latter is more succinct, the former is easier to debug if you use a symbolic debugger. You could replace the test ( check != EOF ) with ( check != BUFFER_SIZE ), to check for short records.

    Clifford
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    107
    Rep Power
    12
    thanks Clifford


    this disappeared off the page before I had time to read it :(

IMN logo majestic logo threadwatch logo seochat tools logo