Printing with fprintf on Win XP

F

Frederick Williams

I'm trying to print to a LaserJet P1006 plugged into a USB port with
this:

#include <stdio.h>

int main(int argc, char *argv[])
{
FILE *p;
int tmp;

p = fopen("USB001", "w");

tmp = fprintf(p, "Sample\n");
if (tmp == EOF)
puts("EOF");
else
printf("Return: %d.\n", tmp);

return 0;
}

tmp == 7 which is gratifying, but nothing comes out of the printer
(which otherwise works fine). In place of "USB001", I've tried
"USB002", "prn:", "prn", "lpt:file.txt", "USB001:file.txt" and
"USB002:file.txt" with the same (lack of) effect. "USB001:", "lpt:" and
"USB002:" cause the MS Visual C debugger to complain that the string
"USB001:", "lpt:" or "USB002:" is null.

Any thoughts?
 
L

Lew Pitcher

I'm trying to print to a LaserJet P1006 plugged into a USB port with
this:

#include <stdio.h>

int main(int argc, char *argv[])
{
FILE *p;
int tmp;

p = fopen("USB001", "w");

tmp = fprintf(p, "Sample\n"); [snip]

Any thoughts?

A couple:

1) Most printers in use these days have some "intelligence", and require
their data streams to be formatted in a particular manner. It is rare to
find a printer that takes a simple text string and reproduces it verbatum.
Print drivers wrap such strings in all sorts of formatting characters, just
to get the printer to print. Its no surprise that you can't get a LaserJet
to print a simple string; you probably need to add lots of
LaserJet-specific formatting around it.

2) Microsoft Windows rarely permits direct application access to devices.
While the name "USB001" may be externalized by Windows, it is unlikely that
Windows actually will permit you to write to the file. I notice that you do
not check the results of fopen(), but instead blindly assume that
fopen() "worked", and has provided you with a valid, writable, FILE handle.
This may not be the case, for many reasons. You should check the results of
fopen(), and if the results are NULL (indicating an error), check the value
of errno to determine the actual error condition.

3) You probably want to fopen() in binary write mode ("wb") rather than text
write mode ("w"). Remember, you'll have to supply the printer-requisite
binary formatting around your print data, and that will likely
require "binary" mode transfer.

HTH
 
L

Lew Pitcher

Following up with more information...

I'm trying to print to a LaserJet P1006 plugged into a USB port with
this:

#include <stdio.h>

int main(int argc, char *argv[])
{
FILE *p;
int tmp;

p = fopen("USB001", "w");

tmp = fprintf(p, "Sample\n"); [snip]

Any thoughts?

A couple:

1) Most printers in use these days have some "intelligence", and require
their data streams to be formatted in a particular manner. It is rare to
find a printer that takes a simple text string and reproduces it verbatum.
Print drivers wrap such strings in all sorts of formatting characters,
just to get the printer to print. Its no surprise that you can't get a
LaserJet to print a simple string; you probably need to add lots of
LaserJet-specific formatting around it.

It appears that the HP LaserJet P1006 uses formatting called
the "Zenographics ZjStream wire protocol". Your code is going to have to
duplicate that protocol in order to get data to the printer, and have the
printer print it. The open source Foo2HP Zjstream print system
(http://foo2hp.rkkda.com/) has some documentation that might help you.

[snip]
 
L

Lew Pitcher

Following up with more information...

I'm trying to print to a LaserJet P1006 plugged into a USB port with
this:

#include <stdio.h>

int main(int argc, char *argv[])
{
FILE *p;
int tmp;

p = fopen("USB001", "w");

tmp = fprintf(p, "Sample\n"); [snip]

Any thoughts?

A couple:

1) Most printers in use these days have some "intelligence", and require
their data streams to be formatted in a particular manner. It is rare to
find a printer that takes a simple text string and reproduces it
verbatum. Print drivers wrap such strings in all sorts of formatting
characters, just to get the printer to print. Its no surprise that you
can't get a LaserJet to print a simple string; you probably need to add
lots of LaserJet-specific formatting around it.

It appears that the HP LaserJet P1006 uses formatting called

correction: the "XQX stream protocol"
Your code is going to have to
duplicate that protocol in order to get data to the printer, and have the
printer print it. The open source

correction: Foo2XQX print system (http://foo2xqx.rkkda.com/)
has some documentation that might help you.

[snip]
 
K

Keith Thompson

Lew Pitcher said:
I'm trying to print to a LaserJet P1006 plugged into a USB port with
this: [...]
p = fopen("USB001", "w");

tmp = fprintf(p, "Sample\n");
[...]
2) Microsoft Windows rarely permits direct application access to devices.
While the name "USB001" may be externalized by Windows, it is unlikely that
Windows actually will permit you to write to the file. I notice that you do
not check the results of fopen(), but instead blindly assume that
fopen() "worked", and has provided you with a valid, writable, FILE handle.
This may not be the case, for many reasons. You should check the results of
fopen(), and if the results are NULL (indicating an error), check the value
of errno to determine the actual error condition.

And it's entirely possible that fopen() will succeed -- and create a
file called "USB001" in your current directory. If there is a special
file name that corresponds to your printer, you should read your
system's documentation to find out what that name is. Guessing is
unlikely to be a successful strategy.
3) You probably want to fopen() in binary write mode ("wb") rather than text
write mode ("w"). Remember, you'll have to supply the printer-requisite
binary formatting around your print data, and that will likely
require "binary" mode transfer.

If your goal is to print something, unless you're implementing a printer
driver, you might be better off writing to a text file and then invoking
some internal program to print it.

Many programs on interactive multi-user and/or multi-processing
systems (including Windows, Unix-like systems, and others) provide
an option to "print" the current file to a specified printer, but
what they really do is submit a job to a queue and let the operating
system take care of printing the file when it's good and ready.
Allowing user programs to write directly to the printer would mess
that up.

Or maybe your system allows you to write directly to the printer,
or provides a name that lets you pretend you're writing directly
to the printer. If so, it's an OS issue, not a C issue.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top