getc() and EOF

S

stephen.r.gray

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
 
W

Walter Roberson

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.
 
S

Stephen

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
 
S

Stephen

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
 
P

Peter Nilsson

...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.
 
S

santosh

Stephen said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top