473,791 Members | 3,074 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Testing for EOF in file with binary data

Greetings!

I am working on a C app that needs to read print files generated by lp
that contain HP LaserJet PCL codes. If the PCL contains binary data to
define a bit map to be printed on the page, either icon or soft-font
download, the EOF test ends prematurely and does not read the entire
file. How do I get around testing for EOF and not detect the EOF value
within the file? Am I missing something?

More Data:

If the print file just contains text data or does not have a bit map
value, the print file is read correctly.

Suggestions?

Thanks in advance!

David Warner
--
comp.lang.c.mod erated - moderation address: cl**@plethora.n et -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
May 11 '06 #1
22 4254
[cross-posting deleted]

David Warner wrote:
I am working on a C app that needs to read print files generated by lp
that contain HP LaserJet PCL codes. If the PCL contains binary data to
define a bit map to be printed on the page, either icon or soft-font
download, the EOF test ends prematurely and does not read the entire
file. How do I get around testing for EOF and not detect the EOF value
within the file? Am I missing something?


We prefer std::fstreams in this group, but the same solution can work
with C or C++ style file streams: use the file size rather than the EOF
character to determine how much data you read. You can use
fstream::seekg( ) and fstream::tellg( ) in C++ or the C equivalents to
get that info.

Cheers! --M

May 11 '06 #2
"mlimber" <ml*****@gmail. com> wrote in message
news:11******** **************@ g10g2000cwb.goo glegroups.com.. .
[cross-posting deleted]

David Warner wrote:
I am working on a C app that needs to read print files generated by lp
that contain HP LaserJet PCL codes. If the PCL contains binary data to
define a bit map to be printed on the page, either icon or soft-font
download, the EOF test ends prematurely and does not read the entire
file. How do I get around testing for EOF and not detect the EOF value
within the file? Am I missing something?


We prefer std::fstreams in this group, but the same solution can work
with C or C++ style file streams: use the file size rather than the EOF
character to determine how much data you read. You can use
fstream::seekg( ) and fstream::tellg( ) in C++ or the C equivalents to
get that info.


The real answer is that the return from fgetc and friends in an int,
not a char. The value of EOF is distinguishable from any char value
you read from a binary stream.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
May 11 '06 #3
In message <Io************ ********@gigane ws.com>, P.J. Plauger
<pj*@dinkumware .com> writes
"mlimber" <ml*****@gmail. com> wrote in message
news:11******* *************** @g10g2000cwb.go oglegroups.com. ..
[cross-posting deleted]

David Warner wrote:
I am working on a C app that needs to read print files generated by lp
that contain HP LaserJet PCL codes. If the PCL contains binary data to
define a bit map to be printed on the page, either icon or soft-font
download, the EOF test ends prematurely and does not read the entire
file. How do I get around testing for EOF and not detect the EOF value
within the file? Am I missing something?


We prefer std::fstreams in this group, but the same solution can work
with C or C++ style file streams: use the file size rather than the EOF
character to determine how much data you read. You can use
fstream::seekg( ) and fstream::tellg( ) in C++ or the C equivalents to
get that info.


The real answer is that the return from fgetc and friends in an int,
not a char. The value of EOF is distinguishable from any char value
you read from a binary stream.


The OP hasn't actually made it clear whether his "EOF" means the macro
or is just an abbreviation for "end-of-file condition", Is he actually
comparing a char against the macro EOF, or is he calling the feof()
function?

In the latter case, the first question is: has he actually opened the
file in binary mode? In text mode, there's at least one OS out there
that will interpret control-Z as end-of-file and produce a premature
termination.

--
Richard Herring
May 15 '06 #4
"Richard Herring" <ju**@[127.0.0.1]> wrote in message
news:Fs******** ******@baesyste ms.com...
In message <Io************ ********@gigane ws.com>, P.J. Plauger
<pj*@dinkumware .com> writes
"mlimber" <ml*****@gmail. com> wrote in message
news:11****** *************** *@g10g2000cwb.g ooglegroups.com ...
[cross-posting deleted]

David Warner wrote:
I am working on a C app that needs to read print files generated by lp
that contain HP LaserJet PCL codes. If the PCL contains binary data to
define a bit map to be printed on the page, either icon or soft-font
download, the EOF test ends prematurely and does not read the entire
file. How do I get around testing for EOF and not detect the EOF value
within the file? Am I missing something?

We prefer std::fstreams in this group, but the same solution can work
with C or C++ style file streams: use the file size rather than the EOF
character to determine how much data you read. You can use
fstream::seekg( ) and fstream::tellg( ) in C++ or the C equivalents to
get that info.
The real answer is that the return from fgetc and friends in an int,
not a char. The value of EOF is distinguishable from any char value
you read from a binary stream.


The OP hasn't actually made it clear whether his "EOF" means the macro or
is just an abbreviation for "end-of-file condition", Is he actually
comparing a char against the macro EOF, or is he calling the feof()
function?


Looked pretty clear to me that he's reading a character, storing it in
a char, then comparing it to EOF -- which of course destroys the
important difference between (int)EOF and (char)EOF. But then I might
be interpolating over aggressively.
In the latter case, the first question is: has he actually opened the file
in binary mode? In text mode, there's at least one OS out there that will
interpret control-Z as end-of-file and produce a premature termination.


Right, and those systems present an early end-of-file condition, which
is different from the OPs concern that (char)EOF look like an early
end-of-file.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
May 15 '06 #5
In article <cl************ ****@plethora.n et>,
da******@westba se.com says...
Greetings!

I am working on a C app that needs to read print files generated by lp
that contain HP LaserJet PCL codes. If the PCL contains binary data to
define a bit map to be printed on the page, either icon or soft-font
download, the EOF test ends prematurely and does not read the entire
file. How do I get around testing for EOF and not detect the EOF value
within the file? Am I missing something?


It's easiest to help if you include a minimal piece of
code that demonstrates the problem.

One thing that often causes this is code something like:

char ch;

while (EOF!=(ch=getc( file)))
process(ch);
the crucial point here is that ch is a char -- for things
to work correctly, it really needs to be an int.

Another possible problem is the way the OS handles the
file you're reading from. A stream will often have a
"cooked" and a "raw" mode. In cooked mode, certain
control codes (e.g. ctrl-D on UNIX and ctrl-C on Windows)
are given special treatment; in raw mode, everything is
passed through without interpretation. This _tends_ to
arise most often if you do something like redirecting
standard input to read from a binary file. Changing this
is usually OS-specific.

--
Later,
Jerry.

The universe is a figment of its own imagination.
--
comp.lang.c.mod erated - moderation address: cl**@plethora.n et -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
May 22 '06 #6
David Warner <da******@westb ase.com> wrote:
I am working on a C app that needs to read print files generated by lp
that contain HP LaserJet PCL codes. If the PCL contains binary data to
define a bit map to be printed on the page, either icon or soft-font
download, the EOF test ends prematurely and does not read the entire
file. How do I get around testing for EOF and not detect the EOF value
within the file?


fopen() it in binary rather than text mode.

Richard
--
comp.lang.c.mod erated - moderation address: cl**@plethora.n et -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
May 22 '06 #7
David Warner wrote:
Greetings!

I am working on a C app that needs to read print files generated by lp
that contain HP LaserJet PCL codes. If the PCL contains binary data to
define a bit map to be printed on the page, either icon or soft-font
download, the EOF test ends prematurely and does not read the entire
file. How do I get around testing for EOF and not detect the EOF value
within the file? Am I missing something?

That you have posted this to multiple newsgroups and in some of them at
least it is off-topic. I am posting this from comp.lang.c++ and it is
off-topic here.
--
comp.lang.c.mod erated - moderation address: cl**@plethora.n et -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
May 22 '06 #8
David Warner wrote:
Greetings!

I am working on a C app that needs to read print files generated by lp
that contain HP LaserJet PCL codes. If the PCL contains binary data to
define a bit map to be printed on the page, either icon or soft-font
download, the EOF test ends prematurely and does not read the entire
file. How do I get around testing for EOF and not detect the EOF value
within the file? Am I missing something?

More Data:

If the print file just contains text data or does not have a bit map
value, the print file is read correctly.

Suggestions?


1. Try to fopen a file using binary option, e.g. "rb". Reading a file
as ASCII can confuse the I/O functions into thinking that the file had
prematurely ended.
2. If you use a test '((c=fgetc(F)) != EOF)', declare 'c' as an int,
not as a char. Another option is to avoid testing every character on
EOF, and using fread instead. When you wouldn't be able to read the
number of chars you asked in fread, the most probable reason is end of
file condition. If you want to be safe, you can test this condition
using feof function.

Michael
--
comp.lang.c.mod erated - moderation address: cl**@plethora.n et -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
May 22 '06 #9
David Warner wrote:
Greetings!

I am working on a C app that needs to read print files generated by lp
that contain HP LaserJet PCL codes. If the PCL contains binary data to
define a bit map to be printed on the page, either icon or soft-font
download, the EOF test ends prematurely and does not read the entire
file. How do I get around testing for EOF and not detect the EOF value
within the file? Am I missing something?

More Data:

If the print file just contains text data or does not have a bit map
value, the print file is read correctly.

Suggestions?

Thanks in advance!

David Warner


You haven't given much information, so I am having to guess a bit, but
I suspect you have fallen into the trap of using a char to store the
characters that you read in. Seems obvious to do, doesn't it? But
consider the following innocent-looking bit of code:

char ch;

ch = getc(fp);

On my system, a char can hold 256 different values - the same number as
there are of normal characters. getc will return a different value to
indicate end of file - but because it is stored in a char it becomes
the same as one of the normal characters before you get a chance to
compare it to anything. In my case, it can't tell the difference
between a -1 indicating the end of the file, and a character 255
validly read from the file.

Getc and the like return an int, and you need to store the value in an
int. Int is big enough to hold all the noraml characters and something
different to indicate end of file.

Hope this helps!

Paul.
--
comp.lang.c.mod erated - moderation address: cl**@plethora.n et -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
May 22 '06 #10

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

Similar topics

3
641
by: David Warner | last post by:
Greetings! I am working on a C app that needs to read print files generated by lp that contain HP LaserJet PCL codes. If the PCL contains binary data to define a bit map to be printed on the page, either icon or soft-font download, the EOF test ends prematurely and does not read the entire file. How do I get around testing for EOF and not detect the EOF value within the file? Am I missing something? More Data:
7
1953
by: Tom | last post by:
Hi all, I am looking for a smart way to assure a file is indeed a text file within a C# method and not binary. For example: Will "thisMysteryFile.dat" be legible if opened in a RichTextBox ... or is it a binary file? I have searched various methods in the string class and am having no luck.
0
9517
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
10207
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10156
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
9997
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...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
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();...
0
5435
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2916
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.