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. ------