Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Printing with fprintf on Win XP

Reply
Thread Tools

Printing with fprintf on Win XP

 
 
Frederick Williams
Guest
Posts: n/a
 
      07-29-2010
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?

--
I can't go on, I'll go on.
 
Reply With Quote
 
 
 
 
Lew Pitcher
Guest
Posts: n/a
 
      07-29-2010
On July 29, 2010 11:53, in comp.lang.c, wrote:

> 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
--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576
Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
---------- Slackware - Because I know what I'm doing. ------


 
Reply With Quote
 
 
 
 
Lew Pitcher
Guest
Posts: n/a
 
      07-29-2010
Following up with more information...

On July 29, 2010 12:12, in comp.lang.c, wrote:

> On July 29, 2010 11:53, in comp.lang.c,
> wrote:
>
>> 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]
--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576
Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
---------- Slackware - Because I know what I'm doing. ------


 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      07-29-2010
On July 29, 2010 12:18, in comp.lang.c, wrote:

> Following up with more information...
>
> On July 29, 2010 12:12, in comp.lang.c, wrote:
>
>> On July 29, 2010 11:53, in comp.lang.c,
>> wrote:
>>
>>> 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]


--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576
Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
---------- Slackware - Because I know what I'm doing. ------


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      07-29-2010
Lew Pitcher <> writes:
> On July 29, 2010 11:53, in comp.lang.c, wrote:
>> 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.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Applet to run on Win 98, Win ME, Win XP, Win Vista & Win 7 ?? Krist Java 6 05-06-2010 11:53 PM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:52 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:50 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:28 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-18-2007 10:11 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57