Sending Output to the Printer
How you print is now a function of your operating system. To print using Windows 95/98 or NT, you have to get your output to a print manager in the operating system.
To direct output to a printer, you must first open a file stream to send output to. You can do this using the same method that you use to send output to a file. First, declare a variable of type fstream. Then open the stream using the member function, open. Instead of opening the stream using the name of a file, use the name of your printer port, for example, "LPT1."
Once a file stream is open, you can print using the name of the stream variable followed by the << operator and the data you want to send to the printer. Be sure to close the stream when you no longer need to use it.
The example program below will send text to a printer on port LPT1.
#include <fstream.h>
int main()
{
ofstream print; // stream variable declaration
print.open("LPT1"); // open stream
// Print Text (the character ‘\f’ will produce a form feed)
print << "This text will print on the printer.\f";
print.close(); // close stream
return 0;
}
Important Note:
Blindly directing text to a printer port assumes some things are true.
It assumes that you have a printer attached to the specified port or that the port is being captured and redirected to a printer.
It assumes that the printer is capable of accepting plain text. For example, a PostScript printer will not print unless the data comes through a PostScript printer driver. The code above does not make use of any drivers.