Be aware laser printers wont work with this program as inkjet/dotmatrix printers use the normal ascii character format whereas laser printers use some crazy ass format ive never worked with and im not sure itll work with USB printers either, but give it a whirl. I’ve seen this asked quite a few times on various forums and its one of the reasons i use iostream/cout over stdio/printf (although this can be done in the latter, its a pain in the ass) “how do i print from the console” the program itself is insanely short:
#include
int main()
{
int a;
cout << "Initialising..." << endl;
ofstream print;
print.open("lpt1:", ios::out);
cout << "Stream opened, sending text..." << endl;
print << "Hello World" << endl;
for(a = 0; a < 6; a++)
{
print << a << endl;
}
print << "f";
print.close();
return 0;
}
If you’ve ever done file handling via streams this wont look so foreign, its pretty much the same, instead of opening the stream to a file on disk you open it to the printer port (assuming lpt1is your printer port). on the line:
“ofstream print;”
You’re basically declaring a stream with the name ‘print’ the next line:
“print.open("lpt1:", ios::out);”
Opens a stream to lpt1 the ios::out denotes the mode of transfer (in or out) and we’d have a job taking input from a printer, look at the line:
“print << "Hello World" << endl;”
Note we use ‘print’ and not cout, this is because ‘print’ is the name of our stream and we can use that to send stuff to the printer just like you would use “cout” to send stuff to the screen as demonstrated in the for loop, the last two lines eject the paper from the printer and close the stream, easy enough.
This article was originally written by Pigsbig78 |