advice on feof()

This is a discussion on advice on feof() within the C Programming forums, part of the General Programming Boards category; I've written a program which sorts variable sized records on a binary file into customer number order. I'm using feof() ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    21

    advice on feof()

    I've written a program which sorts variable sized records on a binary file
    into customer number order. I'm using feof() to terminate my loops, which
    works and I get the required results. I'm just wondering if I'm using feof
    in the right place and correctly.

    The program goes something like this

    in main there's this loop
    Code:
    while(1)
    {
      open files: 
      alternate input file and exchange file, read from one, write to the other
    
      read first character from input file /*to determine what type of record*/
    
      if(!feof(input file)
      {
        read the record into union 'a' /*a union of structures*/
        sort_function(...)
      }
      else
      {
        close input and exchange files
        break;
      }
    
    }
    in the sort function
    Code:
    while(1)
    {
      read next character from input file
    
      if(!feof(input file)
      {
        read the record into union 'b'
        compare customer numbers 'a' & 'b'
        write record with highest customer number to exchange file
      }
      else
        break;
    }
    write record with lowest customer number to sort file
    close input and exchange files
    I've searched the board for other posts on feof and I get the feeling It's maybe
    not a good idea to use it. When I first wrote the program I used the return value of
    fread to terminate the loop, but I had read that it returns zero on end of file or error
    and you should use feof to determine which.

    Hope this isn't too vague. Thanks for any advice you can give.


    hobo

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Well, the best way to do it, is to test the return value of your function that reads the input. If it fails, then either there was an error, or there was nothing left to read (Actually both would be an error).

    Here's what I mean:

    Code:
      if(read the record into union 'a' == TRUE)
      {
        /*a union of structures*/
        sort_function(...)
      }
      else
      {
        close input and exchange files
        break;
      }

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    21
    Thanks for your response, either way it produces the same results, but I can see why testing the return value of the read function is the better way to go.
    Thanks again.

    hobo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need advice about list of lists
    By handytxg in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2009, 06:03 PM
  2. Advice on C Programming with MSVC++ 2008
    By IT_Guy in forum Windows Programming
    Replies: 1
    Last Post: 03-06-2009, 04:23 AM
  3. game engine advice?
    By stien in forum Game Programming
    Replies: 0
    Last Post: 01-23-2007, 03:46 PM
  4. girl friend advice (prob. the wrong place)
    By B0bDole in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 10-22-2004, 07:38 PM
  5. need advice on project
    By lambs4 in forum C Programming
    Replies: 2
    Last Post: 07-23-2003, 02:06 PM

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