• C

printing from a C based program

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 -
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent OlsenData Warehouse Architect / DBACommented:

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

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

Jaime OlivaresSoftware ArchitectCommented:
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");
Kent OlsenData Warehouse Architect / DBACommented:
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,

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

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

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.