It is a good thing I keep this handy... this question comes up every week.
Code:
#include <iostream>
#include <fstream>
#include <stdlib>
using namespace std ;
int main()
{
char printer[10] = "LPT1:";
char character;
ofstream prnt (printer);
if (prnt.fail())
{
cout << "ERROR-Unable to open " << printer << '\n' ;
return 1 ;
}
system ("cls");
cout << "Type the text you wish to have printed. You must use "
<< "\nyour own returns as there is no text wrapping other than "
<< "\nthat which the printer will do at the end of page. Though, "
<< "\nthis may lead to words being cut in half. This simple "
<< "\nprogram could be made much better utilizing iomanip for text "
<< "\nformatting, but it shows you the basics of opening a printer "
<< "\nport. " << endl << endl
<< "Press a '#' and return when you are ready to print... " << endl
<< endl << endl;
while (character != '#')
{
cin.get(character);
if(character != '#')
prnt.put(character);
}
prnt << '\r' << '\f' ; // return and eject the last page from the printer
prnt.close();
return(0);
}