printing from a C based program

Posted on 2004-11-29
Medium Priority
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 46

Expert Comment

by:Kent Olsen
ID: 12700108

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

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

LVL 55

Expert Comment

by:Jaime Olivares
ID: 12700302
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 46

Accepted Solution

Kent Olsen earned 1000 total points
ID: 12700510
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

ID: 12703792

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


Expert Comment

ID: 12704110

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

862 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