473,387 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

getc() and EOF

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 '07 #1
5 4101
In article <11**********************@m58g2000cwm.googlegroups .com>,
<st************@gmail.comwrote:
>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
Jan 26 '07 #2


On Jan 26, 2:15 pm, rober...@ibd.nrc-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

Jan 26 '07 #3
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

Jan 26 '07 #4
On Jan 27, 7:15 am, rober...@ibd.nrc-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

Jan 26 '07 #5
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.

Jan 27 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Andrew Kibler | last post by:
Two sections of code, in the first one fwrite works, in the second one it doesn't (ms VC++) both are identical except in the working one fseek is used twice to set the file pointer, once just...
9
by: spike | last post by:
I use this code to read number of characters from a file. The problem is: It only returns about 1500 chars. If i open the file in a text editor it contains almoust 3 million rows. Sure it must...
13
by: William L. Bahn | last post by:
I'm sure this has been asked before, and I have looked in the FAQ, but I'm looking for an explanation for the following: The functions pairs: gets()/fgets() puts()/fputs() printf()/fprintf()...
8
by: Bill Cunningham | last post by:
Would getc and ungetc be the best and most simple what to parse expressions for a parser? Bill
11
by: TTroy | last post by:
Hello C programmers, Can someone tell me why ungetc can't sent back EOF, but it's sister function getc has no trouble sending it to us? For a file, this might not make a difference, but for an...
19
by: mailursubbu | last post by:
HI, Below is my program. I compiled it through g++. Now strange thing is, getc is not reading the data instead its printing the previously read data ! Please some one let me know whats wrong. ...
7
by: hzmonte | last post by:
My C program has the following: static int skip_space(FILE *fp, int *line, int c) { int i = 0; if(feof(fp)) { printf("in skip feof ...\n"); } printf("in skip start fp=%p line=%d c=%d\n", fp,...
5
by: Richard Weeks | last post by:
Below is a fragment from a program that calculates statistics on x,y data. I want the user to be able to predict one or more predicted values of y from x, given the line of best fit. I have a...
2
by: Praesidium | last post by:
I'm having a bit of a problem running a C program I'm working on (compiled in cygwin). There's a way to go, as I'm intent on making sure each bit works before moving on to the next. As it appears...
32
by: vippstar | last post by:
Assuming all the values of int are in the range of unsigned char, what happends if getc returns EOF? Is it possible that EOF was the value of the byte read? Does that mean that code aiming for...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.