printing from a C based program

Posted on 2004-11-29
Last Modified: 2010-04-15
I am working on a hand held computer (bar code scanner) that can be cabled to a printer. The unit will run 'C' based programs on it. Do you have a sample 'C' program that will print say 'Hello World' to a printing device attached to the computer. I know this hardware configuration works, 'cause the vendor shipped some software (with out the source of course) that kind of does what I want, but I need to come up with my own variant. I've programmed for 20 years in Cobol, Assembler, RPGIII, Basic and VB but this is my first trip down the "C" road. I will want to eventually print bar codes to this printer, but lets cross one bridge at a time. Any suggestions will be greatly appreciated. Thanks! - ERic -
Question by:carpetking
    LVL 45

    Expert Comment


    #include <stdlib.h>
    #include <stdio.h>

    main ()
      printf (stdout, "Hello, World.\n");

    LVL 55

    Expert Comment

    by:Jaime Olivares
    you can try to open LPT1 or PRN port as a file, then use printf(). Something like

    FILE *printer;

    printer = fopen("LPT1:", "wb");
    if (printer) {
        fprintf (printer, "Printer test\n");
        /* some more fprintf function calls */
    } else
       printf("Error while trying to open printer port\n");
    LVL 45

    Accepted Solution

    Hi Carpetking,

    Both jaime and I are speaking somewhat "tongue in cheek".  Depending on the underlying operating system, you may want to print directly to the printer, but if you're using even a quasi-mature O/S, you probably should write to a file and let the spooler handle the printing.

    Most/many unix/c environments include the stream 'stdprn'.  It's not universal, but it is available in a lot of places.

    As Jaime indicated, you can open a stream directly to the printer.  Be careful, the name of the device will vary with your operating system.  (Unix will probably want something like "/dev/lp" for a file name.)

    Writing directly to the printer may have a serious drawback.  You'll have to make special provision for writing to the printer and handling errors.  You probably don't want your application to hang up if the printer jams.  (Then again, you might want it to hang if driving the printer is its primary function.)

    Writing to a separate file and letting the spooler print the file is often the preferred method for writing to a printer.

    Good Luck,
    LVL 16

    Expert Comment


    What kind of Barcode scanner? Ive done work with Symbol and Casio. I may be able to help with others.

    LVL 8

    Expert Comment


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
    Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
    The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now