473,608 Members | 2,054 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

end-of-file problem

Hello,

I read a simple bmp-file with this loop:

while ( !feof(fp) ) {
printf("%x\n", fgetc(fp));
}
fclose(fp);

Everything seems to be correct, but at the end of the file, I get a weird
"ffffffff" output. How does this come? Actually the loop must have been
finished before printf can print some data, or when will the file
pointer increased in this case?
Thanks,
Markus
Aug 20 '06 #1
29 3446
boa
* Markus Pitha wrote, On 20.08.2006 15:01:
Hello,

I read a simple bmp-file with this loop:

while ( !feof(fp) ) {
printf("%x\n", fgetc(fp));
}
fclose(fp);

Everything seems to be correct,
It isn't, see the faq for details. http://c-faq.com/stdio/feof.html
but at the end of the file, I get a weird
"ffffffff" output. How does this come? Actually the loop must have been
finished before printf can print some data, or when will the file
pointer increased in this case?

ffffffff happens to be the same as -1 on most machines. EOF also happens
to be -1 on most machines. So you're printing the value of EOF, returned
from fgetc(). On the next iteration, feof() returns true and your loop
stops.

boa
>

Thanks,
Markus
Aug 20 '06 #2
Hello,

thanks for the link but I couldn't handle it as in this example. I
got warnings of comparing pointers with NULL and therefore a bad program
behaviour like endless loops.
Now I decided to use the following construct:

fseek(fp, 0, SEEK_END);
filesize = ftell(fp);
rewind(fp);

while (ftell(fp) < filesize) {
printf("%x\n", fgetc(fp));
}
fclose(fp);
ffffffff happens to be the same as -1 on most machines.
to be -1 on most machines. So you're printing the value of EOF, returned
from fgetc(). On the next iteration, feof() returns true and your loop
stops.
Thanks, that sounds plausible to me.
Aug 20 '06 #3
boa
* Markus Pitha wrote, On 20.08.2006 16:36:
Hello,

thanks for the link but I couldn't handle it as in this example. I
got warnings of comparing pointers with NULL and therefore a bad program
behaviour like endless loops.
Now I decided to use the following construct:

fseek(fp, 0, SEEK_END);
filesize = ftell(fp);
rewind(fp);

while (ftell(fp) < filesize) {
printf("%x\n", fgetc(fp));
}
fclose(fp);

Please don't.

How about something like this instead?
int c;

while( (c = fgetc(fp)) != EOF)
printf("%x\n"; c);
Boa

[snip]
Aug 20 '06 #4
boa wrote:
while( (c = fgetc(fp)) != EOF)
printf("%x\n"; c);
I thought this is only allowed with text files because of the fact that
EOF is defined as -1 and binary files could contain "-1"?
Aug 20 '06 #5
Markus Pitha wrote:
boa wrote:
while( (c = fgetc(fp)) != EOF)
printf("%x\n"; c);

I thought this is only allowed with text files because of the fact that
EOF is defined as -1 and binary files could contain "-1"?
No , it is allowed with all files. EOF will be defined in a
way that the value cannot appear in a file on your platform.

Aug 20 '06 #6
Markus Pitha wrote:
boa wrote:
> while( (c = fgetc(fp)) != EOF)
printf("%x\n"; c);

I thought this is only allowed with text files because of the fact that
EOF is defined as -1 and binary files could contain "-1"?
fgetc() always returns either an unsigned char converted to int, or EOF. As
long as c is wide enough to hold both EOF and 0 ... UCHAR_MAX, for example
with c declared as int as it was in the message you replied to, there's no
problem. With c declared as char, there would be a problem.
Aug 20 '06 #7
Markus Pitha wrote:
>
I read a simple bmp-file with this loop:

while ( !feof(fp) ) {
printf("%x\n", fgetc(fp));
}
fclose(fp);

Everything seems to be correct, but at the end of the file, I get
a weird "ffffffff" output. How does this come? Actually the loop
must have been finished before printf can print some data, or when
will the file pointer increased in this case?
Nothing weird about it. It is simple misuse of the feof function.

int ch;

while (EOF != (ch = getc(fp)) {
printf("%x\n", fgetc(fp));
}
if (!feof(fp)) puts("Hardware error occured");

feof does not look forward, as in better languages. It
distinguishes between i/o errors and reaching EOF when an input
statement fails. To distinguish between EOF and all normal chars
it is necessary to receive those chars into an int, rather than a
char.

getc is normally preferable to fgetc when the file argument can be
evaluated more than once.

--
Chuck F (cb********@yah oo.com) (cb********@mai neline.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.netUSE maineline address!
Aug 20 '06 #8
Harald van Dijk wrote:
fgetc() always returns either an unsigned char converted to int, or EOF. As
long as c is wide enough to hold both EOF and 0 ... UCHAR_MAX, for example
with c declared as int as it was in the message you replied to, there's no
problem. With c declared as char, there would be a problem.
I understand, thanks to everybody.
Markus
Aug 20 '06 #9
CBFalconer wrote:
Markus Pitha wrote:
>I read a simple bmp-file with this loop:

while ( !feof(fp) ) {
printf("%x\n", fgetc(fp));
}
fclose(fp);

Everything seems to be correct, but at the end of the file, I get
a weird "ffffffff" output. How does this come? Actually the loop
must have been finished before printf can print some data, or when
will the file pointer increased in this case?

Nothing weird about it. It is simple misuse of the feof function.

int ch;

while (EOF != (ch = getc(fp)) {
printf("%x\n", fgetc(fp));
Did you really mean to read another character and print it, thereby
printing every other character? * think you meant:
printf("%x\n", ch);
}
if (!feof(fp)) puts("Hardware error occured");

feof does not look forward, as in better languages. It
distinguishes between i/o errors and reaching EOF when an input
statement fails. To distinguish between EOF and all normal chars
it is necessary to receive those chars into an int, rather than a
char.
Agreed.
getc is normally preferable to fgetc when the file argument can be
evaluated more than once.
I would say that it is extremely rare to need fgetc rather than getc. I
understand the code constructs which would require it, e.g.
fgetc(fparr[i++), I just can't think of anywhere where they would have
been useful to me. Has anyone here actually done something where they
had to use fgetc rather than getc?
--
Flash Gordon
Still sigless on this computer.
Aug 20 '06 #10

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

Similar topics

3
5270
by: Matt | last post by:
I always heard people saying IIS ASP front end, and MS-SQL back end. ASP is for server side programming and dynamic content generation, how could it is called front end? Because I thought it is executed in the server, which is back end? I think I am confused with the term front end and back end here. Please advise. Thanks!!
2
1459
by: Alistair | last post by:
still working on this DB :o( I have.. a query that I run, if this query is unsuccessful then another query is run on another table, it goes something like this if rs1.EOF then '1st query returns nothing
5
2174
by: gelbeiche | last post by:
Is ( cont.begin() == cont.end() ) essentially equivalent to writing ( cont.empty() ) for a STL container ?
5
10781
by: mitchchristensen | last post by:
I have a transaction log that tracks issues from a call center. Each time an issue is assigned to someone else, closed, etc. I get a time stamp. I have these time stamps for the beginning of an issue to the end of an issue and I'd like to determine how many business hours these issues were open. Issue BeginDt Enddt Total hours 1 3/29/05 5:00 PM 4/1/05 2:00 PM 69 ...
2
4496
by: Jeff Pritchard | last post by:
Some time ago I am sure I came across something that said this was possible, though it doesn't seem to work. A client wants to replace an Access back-end with SQL Server tables. We have tried linking the SQL tables to an Access back-end and then linking the linked tables in the Access back-end to the Access front-end. Doesn't work at all. Obviously, this can't be done. What is the best way to replace an Access back-end with SQL tables?
4
2796
by: John Baker | last post by:
Hi: I have an application that has a back end and a front end. I need to add a table which is identical to one in the back end, and then use it for a temporary holing place form some records. I have done the following: .. Copied the table in the back end and given the copy a new name .. Created a table with the identical name to the back end copy in the front end .. Used the linkage process to link the table reference in the front end...
8
1474
by: Murt | last post by:
on my inbox below i want the loop to quit if the user enters End, It does not function as written, any ideas? thanks ---------------------- Dim inputString As String
5
1257
by: Ashish | last post by:
Hi, please take a look at the following code. (I've ommitted the Windows generated code) -------------------------------- Imports System.Drawing.Drawing2D Public Class Form1 Inherits System.Windows.Forms.Form Dim points() As Point = {} Dim pen As New Pen(Color.FromArgb(255, 0, 0, 255)) Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
1
1957
by: paul kangethe | last post by:
Public Class frmMain Private Sub btnCalulate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalulate.Click Try Dim LoanAmount As Double Dim Payment As DoubleDim
0
8087
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8025
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8509
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8179
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8365
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6847
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5499
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
1620
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1363
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.