Thread: EOF file pointer

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    2

    EOF file pointer

    Hi,
    I´d like to know if while I´m reading a file I can check wether the next cycle in my While is going to be EOF... I need this because I´m reading 13 byte chunck at a time. sorry for my english. thank you in advance.

    code:
    Code:
    while (ch=fgetc(fp))!=EOF)
                    //check the buffer
                    buffer++;
                    ....
                    if ((buffer<12) && (###)
    ### here I want to check if current position + 1 == EOF

    any answer is welcome

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well if you're reading from a file, you can start by using fseek() and ftell() to work out where the end of the file is.
    You can then count how many bytes you read, and thus work out whether you've reached the end or not.

    Or you could do
    Code:
    char buff[13];
    while ( fread( buff, 13, 1, fp ) == 1 ) {
      // do something with 13 bytes of data
    }
    If there is less than 13 bytes remaining at the end of the file, then fread() won't return them.
    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.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    2
    Thank you very much! I´ll try that way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM