Welcome to Reviews OnLine: Q&A: How do I print from C or C++?
How do I Print from C or C++?
by Smith & Jones (July 1998)

While using Microsoft Visual C++ 5.0 Professional Edition, how do you get output from a standard C++ source file to print to a printer? It seems to only allows output to go to a DOS box which cannot be printed.

The short answer is that it takes only three or four lines of code to print, as long as you are printing to the local printer (a printer directly connected to the parallel port of your computer).

There are three basic ways to print from the C or C++ languages, depending on the type of output desired and the amount of code you want to write.

1. (From C++): Use the ostream class (cout is an ostream) This should work for both a CONSOLE and a Windows application.

Advantages: Easy printing of simple text.

Disadvantages: Can only print to local printer.


filebuf fb( "prn:" ); // Filebuf object attached to "prn:"
filebuf fb( "prn:" ); // Filebuf object attached to "prn:"
cout = &fb; // fb associated with cout
cout << "testing"; // Text goes to PRN: not stdout

2. (From C): Use stdio file functions.

This is exactly equivalent to the stream method without using classes.

Advantages: Easy printing of simple text.

Disadvantages: Can only print to local printer.


FILE * fpPrint; // file handle for printing
fpPrint = fopen( "PRN:" ); // open the printer port
fprintf( fpPrint, "testing" ); // write to the printer
fclose( fpPrint ); // close the printer port

3. (Using the Windows API): Although this method may look pretty to the user, it can be a lot of work to code.

Advantages: More user friendly.
Allows easy graphics printing.
Network printers can be used for printing.

Disadvantages: Requires a lot more setup.

To fully support printing from a Windows application, you'd need to give the user access to standard printer dialogs, the ability to setup up a printer device, and render your graphics into the chosen printer device context, while checking for user cancellation, etc. In other words, it's more work, but can help make your application look more like commercial software.

Code: If you need to do this style of printing, refer to the samples provided with Visual C++, such as the standard SCRIBBLE example.

Another, more complete example can be found in the Visual C++ 5.0 books under:

    = > Windows SDK Samples
       = > Win32 Samples
          = > PRINTER

or, just search for the words "PRINTER sample" in help

Copyright 1998 Smith & Jones All rights reserved.

Article Indexes
  Products Index
  People Index
  Events Index

Product Briefs
Desktop PCs & Servers
  Ergo Thunder Brick 2
Eizo FlexScan Displays
Future Power epower
HP NetServer LXr 8500
Meridian Snap! Server
Digital Cameras
  3Com HomeConnect
Agfa ePhoto 780c
Canon A5 Zoom
Kodak DC265
Minolta 3D 1500
Nikon D1
Olympus C2000
Toshiba PDR-M4 & M5
Notebooks & Mobile
  3Com Palm VII
Fosa Mini Notebook
Pionex Elite
Quantex Celeron Laptop
Sharp Actius PC-A250
Sony F250/F290 Notebooks
Talontek MPC-2000
Mobile/Wireless Communications
  3Com 56K GSM Modem
Novatel Merlin Modem
Xircom Ethernet CompactCard
Mobile Software
  Etak Traffic Touch
DataViz Documents To Go
Softbook E-book System
  Agfa SnapScan Touch
Visioneer Strobe Pro
  Brother MP-21 C Portable
Tektronix Phaser 780
Xerox 480cx
Software & Books
  Avid Cinema for Windows
Caere PageKeeper Pro
Corex Area Code Fix
Docent Outliner
Macmillan Office 2K Books
Paragon FoneSync
Symantec pcAnywhere 9.0
Sybari Antigen 5.5
V-Communications AutoSave
  InFocus 330
Mitsubishi S120
  Club Photo
Intelliquis Traffic Builder
NETSilicon NET+Works
Panasite Web Builder
  Calluna 250 Mb PCCard
Castlewood 2.2Gb ORB
Iomega Clik!
Teac CD-W 54E
Batteries & Power
  Duracell Ultra Battery
Electrofuel PowerPad
Eveready Energizer ACCU
Graphics & Video
  Belkin USB Video Bus
Daeyang VR Headset
Kodak/Intel Picture CD
Nogatech USB TV
Number Nine SR9
  Logitech Cordless mouse
Raritan Switchman
Targus USB MiniMouse
Reviews OnLine Contents About Reviews OnLine Site Index Search Reviews OnLine Site Index Search Reviews OnLine