getc() and EOF

Discussion in 'C Programming' started by stephen.r.gray@gmail.com, Jan 26, 2007.

  1. Guest

    I'm trying to read the contents of a file. I think that I'm getting an
    "end-of-file" condition before the entire file is read. Below is my
    function that reads in the file:

    file_ptr=fopen(twod_file,"rb"); //done elsewhere outside the function
    <CODE>
    ReadEntireFile(file_ptr)
    FILE *file_ptr;
    {
    register one_byte;
    static long int count = 0;
    printf("\n");
    while( ( one_byte = getc(file_ptr) ) != EOF )
    {
    count++;
    printf("%d", one_byte);
    }
    printf("\nerrno = %d", errno);
    printf("\n");
    printf("\ncount = %ld", count);
    printf("\nfeof = %d", feof(file_ptr));
    printf("\nferror = %d", ferror(file_ptr));
    }
    </CODE

    Here is the output (with the file contents removed):

    <OUTPUT>
    errno = 0
    count = 2093056
    feof = 16
    ferror = 0
    </OUTPUT>

    My file size is 2097152 bytes. I have verfied that not all of the
    contents of the file are outputted. It seems as though I get and
    "end-of-file" condition before I read the end of file.

    Any help?

    -Stephen
    , Jan 26, 2007
    #1
    1. Advertising

  2. In article <>,
    <> wrote:
    >I'm trying to read the contents of a file. I think that I'm getting an
    >"end-of-file" condition before the entire file is read. Below is my
    >function that reads in the file:


    >file_ptr=fopen(twod_file,"rb"); //done elsewhere outside the function


    > static long int count = 0;


    Minor caution there: long int could overflow at 2^31-1 .
    Going unsigned would double your potential range. Consider too the
    possibility of using size_t .

    > printf("\n");
    > while( ( one_byte = getc(file_ptr) ) != EOF )
    > {
    > count++;
    > printf("%d", one_byte);
    > }


    >Here is the output (with the file contents removed):
    >
    >count = 2093056


    >My file size is 2097152 bytes. I have verfied that not all of the
    >contents of the file are outputted. It seems as though I get and
    >"end-of-file" condition before I read the end of file.


    2097152 is a suspicious size, in that it is exactly 0x200000.
    2093056 is 0x1ff000 which is 0x1000 (decimal 4096) less -- also
    suspicious.

    What this suggests to me is the -possibility- that the data beyond
    2093056 consists entirely of binary zeroes. One of the oddities of
    binary files in the C standard is that trailing zeroes need not be
    preserved.

    Another possibility, outside the C standard, is that the file was
    created by seeking to 2097152 but nothing was written beyond 2093056.
    In some OS's, if you extend a file by seeking to a point and then
    truncating the file (a common way to create a file of a particular
    size), the empty data between the last written byte and the
    truncatation point might not be readable -- particularily if the
    filesystem supports "holes" in the file.
    --
    If you lie to the compiler, it will get its revenge. -- Henry Spencer
    Walter Roberson, Jan 26, 2007
    #2
    1. Advertising

  3. Stephen Guest

    On Jan 26, 2:15 pm, -cnrc.gc.ca (Walter Roberson)
    wrote:
    > What this suggests to me is the -possibility- that the data beyond
    > 2093056 consists entirely of binary zeroes. One of the oddities of
    > binary files in the C standard is that trailing zeroes need not be
    > preserved.
    >
    > Another possibility, outside the C standard, is that the file was
    > created by seeking to 2097152 but nothing was written beyond
    > 2093056.


    I have verified with a binary file viewer that there is non-zero binary
    data after the 2093056 mark. The file is image data (1024x1024x2
    bytes). It is interesting that it stopped 4096 bytes short...

    Thanks for the reply,
    Stephen
    Stephen, Jan 26, 2007
    #3
  4. Stephen Guest

    Sorry to reply to my own post, but I figured out what was going on.

    I was trying to read from a file that was fopen'ed, written to, and
    never fclose'ed by another function.

    -Stephen
    Stephen, Jan 26, 2007
    #4
  5. On Jan 27, 7:15 am, -cnrc.gc.ca (Walter Roberson)
    wrote:
    > ...One of the oddities of
    > binary files in the C standard is that trailing zeroes need not be
    > preserved.


    There may be _additional_ trailing zeros in a binary file. Trailing
    whitespace before a newline need not be preserved in a text file.

    --
    Peter
    Peter Nilsson, Jan 26, 2007
    #5
  6. santosh Guest

    Stephen wrote:
    > Sorry to reply to my own post, but I figured out what was going on.
    >
    > I was trying to read from a file that was fopen'ed, written to, and
    > never fclose'ed by another function.


    No need to close it. An fseek() to the beginning should be sufficient.
    santosh, Jan 27, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bill Cunningham

    getc and ungetc

    Bill Cunningham, Nov 18, 2004, in forum: C Programming
    Replies:
    8
    Views:
    393
    Malcolm
    Nov 22, 2004
  2. Kobu
    Replies:
    10
    Views:
    620
    Keith Thompson
    Mar 4, 2005
  3. TTroy
    Replies:
    11
    Views:
    727
    Dik T. Winter
    Mar 14, 2005
  4. SpreadTooThin

    ifstream eof not reporting eof?

    SpreadTooThin, Jun 13, 2007, in forum: C++
    Replies:
    10
    Views:
    689
    James Kanze
    Jun 15, 2007
  5. Jan Burse
    Replies:
    67
    Views:
    1,047
    Jan Burse
    Mar 14, 2012
Loading...

Share This Page