Printing with fprintf on Win XP

Discussion in 'C Programming' started by Frederick Williams, Jul 29, 2010.

  1. 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.
    Frederick Williams, Jul 29, 2010
    #1
    1. Advertising

  2. Frederick Williams

    Lew Pitcher Guest

    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. ------
    Lew Pitcher, Jul 29, 2010
    #2
    1. Advertising

  3. Frederick Williams

    Lew Pitcher Guest

    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. ------
    Lew Pitcher, Jul 29, 2010
    #3
  4. Frederick Williams

    Lew Pitcher Guest

    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. ------
    Lew Pitcher, Jul 29, 2010
    #4
  5. 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) <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"
    Keith Thompson, Jul 29, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ARaman
    Replies:
    1
    Views:
    384
    Mike Wahler
    Oct 23, 2003
  2. Chris Lyon
    Replies:
    0
    Views:
    318
    Chris Lyon
    Oct 7, 2003
  3. Ringwraith
    Replies:
    4
    Views:
    944
    Ringwraith
    Jan 27, 2004
  4. Krist
    Replies:
    6
    Views:
    725
    Arne Vajhøj
    May 7, 2010
  5. Peter Singer

    Web Services on Win 2003 to SQL Server on Win 2000

    Peter Singer, Sep 7, 2004, in forum: ASP .Net Web Services
    Replies:
    10
    Views:
    286
    Yan-Hong Huang[MSFT]
    Sep 15, 2004
Loading...

Share This Page